André Luiz
André Luiz

Reputation: 7322

XSLT: select node based on the value of many attributes

I have a XSLT that's used to parse a XML into a json. I pasting below the part I'm trying to retrieve the data from:

<datafield tag="856" ind1="4" ind2="0">
    <subfield code="u">abc</subfield>
</datafield>
<datafield tag="856" ind1="4" ind2="2">
    <subfield code="u">xyz</subfield>
</datafield>

I want to select the data from the node where tag=856 AND ind1=4 AND ind2=2

I managed to do the check if this node exists, but I'm getting the value of both nodes with the following rule:

<xsl:if test="marc:datafield[@tag='856'] and marc:datafield[@ind1='4'] and marc:datafield[@ind2='2']">
    <z:index name="thumbnailUrl_s Thumbnail">
        <xsl:value-of select="marc:datafield[@tag='856']/marc:subfield[@code='u']"/>
    </z:index>
</xsl:if>

Is there a way so my xsl:value-of returns only the node matching the rules I described above? I've been looking on similar answers here in StackOverflow and so far I couldn't find one with the same issue.

Upvotes: 0

Views: 43

Answers (1)

zx485
zx485

Reputation: 29052

You can use this code:

<xsl:if test="marc:datafield[@tag='856' and @ind1='4' and @ind2='2']">
    <z:index name="thumbnailUrl_s Thumbnail">
        <xsl:value-of select="marc:datafield[@tag='856' and @ind1='4' and @ind2='2']/marc:subfield[@code='u']"/>
    </z:index>
</xsl:if>

Or, in a template:

<xsl:template match="marc:datafield[@tag='856' and @ind1='4' and @ind2='2']">
    <z:index name="thumbnailUrl_s Thumbnail">
        <xsl:value-of select="marc:subfield[@code='u']"/>
    </z:index>
</xsl:template>

Upvotes: 1

Related Questions