Reputation: 1675
This has got to be a very basic question, but I'm really struggling with a solution. From the following XML, I'm trying to extract the first instance only, when I have a match on the tag="099"
and the code="a"
attributes in a marc:datafield
node, and a marc:subfield
node, respectively
<?xml version="1.0" encoding="UTF-8" ?>
<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
<marc:record>
...
<marc:datafield tag="079" ind1=" " ind2=" ">
<marc:subfield code="a">79927839</marc:subfield>
</marc:datafield>
<marc:datafield tag="099" ind1=" " ind2=" ">
<marc:subfield code="a">940002</marc:subfield>
</marc:datafield>
<marc:datafield tag="099" ind1=" " ind2=" ">
<marc:subfield code="a">940002*</marc:subfield>
</marc:datafield>
<marc:datafield tag="099" ind1=" " ind2=" ">
<marc:subfield code="a">940002**</marc:subfield>
...
So, I'm trying to retrieve that first "940002". Using the following code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:marc="http://www.loc.gov/MARC21/slim">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/">
<!--identifier-->
<xsl:if test="/marc:collection/marc:record/marc:datafield/@tag='099'">
<xsl:element name="dc:identifier">
<xsl:if test="position()=1">
<xsl:value-of select="/marc:collection/marc:record/marc:datafield/marc:subfield/@code='a'"/>
</xsl:if>
</xsl:element>
</xsl:if>
...
However, what I'm getting is:
<?xml version="1.0" encoding="UTF-8"?>
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:marc="http://www.loc.gov/MARC21/slim">
<dc:identifier>true</dc:identifier>
...
Instead of the desired:
<?xml version="1.0" encoding="UTF-8"?>
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:marc="http://www.loc.gov/MARC21/slim">
<dc:identifier>940002</dc:identifier>
...
I'm guessing that it's got to be an Xpath issue, but... Any leads would be appreciated.
Thanks
Upvotes: 2
Views: 2233
Reputation: 243579
Use:
/marc:collection/marc:record/marc:datafield
[@tag='099']/marc:subfield[@code='a'][1]/text()
Upvotes: 2
Reputation: 1675
Got it! Adding in another select did the trick. Thanks so much for the help!
<xsl:for-each select="/marc:collection/marc:record/marc:datafield[@tag='099']">
<xsl:element name="dc:identifier">`
<xsl:if test="position()=1">
<xsl:value-of select="marc:subfield[@code='a']/text()"/>
</xsl:if>
</xsl:element>
</xsl:for-each>
Upvotes: 1
Reputation: 22064
Change this
<xsl:value-of select="/marc:collection/marc:record/marc:datafield/marc:subfield/@code='a'"/>
to
<xsl:value-of select="/marc:collection/marc:record/marc:datafield/marc:subfield[@code='a']/text()"/>
You're value-of is selecting whether the @code
is equal to 'a'
instead of selecting the marc:subfield
where the @code='a'
Upvotes: 2