views:

46

answers:

3

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"&gt;
<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"&gt;
<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/"&gt;
        <!--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"&gt;
   <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"&gt;
   <dc:identifier>940002</dc:identifier>
   ...

I'm guessing that it's got to be an Xpath issue, but... Any leads would be appreciated.

Thanks

+2  A: 

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'

Chad
Wow, that seems to be more along the right lines, but didn't just select when the `<marc:datafield tag="099">` in the node above it is matched. It gave me a huge concatenated string of every `marc:subfield` with an `@code='a'` in it.
LOlliffe
Try changing `marc:subfield[@code='a']` to `marc:subfield[@code='a'][1]`.
DevNull
@Chad and @LOlliffe: this does not conform the `marc:datafield[@tag='099']` condition. See Dimitre's correct answer.
Alejandro
A: 

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>
LOlliffe
@LOlliffe: you don't need the `xsl:for-each` and test for `position()`. This should be done with one XPath expressions as Dimitre's answer. Also you don't need the `xsl:element`: if you know in advance the name of the element, you should output as a literal result element like: `<dc:identifier><!-- content --></dc:identifier>`
Alejandro
+2  A: 

Use:

/marc:collection/marc:record/marc:datafield
                   [@tag='099']/marc:subfield[@code='a'][1]/text()
Dimitre Novatchev
+1 for the only correct solution.
Alejandro