XSLT - Match Attributes within a group and pulling values of those attributes
I have a group of tags with attributes and I only want to pull certain values into a new group after I match attributes.
The input file looks like this:
<content>
<manifest>
<item id="id1272682" href="ch01.html"/>
<item id="id1272759" href="ch02.html"/>
</manifest>
<spine>
<itemref idref="id1272759"/>
<itemref idref="id1273380"/>
</spine>
</content>
I want my XSLT to look in the spine for any itemref/@idref values (it can be more than one) that match the item/@id in the manifest and add that value to the spine so that the output looks like this:
<spine>
<itemref idref="id1272759"/>
</spine>
Here is what I have thus far. It appears my if: statement is working as I am getting the correct amount of itemref tags but am not getting the value of the idref.
<xsl:if test="itemref[attribute::idref = ../../manifest/item/@id]">
<xsl:element name="itemref">
<xsl:attribute name="idref">
<xsl:value-of select="@id开发者_Python百科ref"/>
</xsl:attribute>
</xsl:element>
</xsl:if>
And my output is:
<spine>
<itemref idref=""/>
</spine>
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="spine">
<spine>
<xsl:copy-of select="itemref[@idref = /*/manifest/item/@id]"/>
</spine>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<content>
<manifest>
<item id="id1272682" href="ch01.html"/>
<item id="id1272759" href="ch02.html"/>
</manifest>
<spine>
<itemref idref="id1272759"/>
<itemref idref="id1273380"/>
</spine>
</content>
produces the wanted, correct result:
<spine>
<itemref idref="id1272759"/>
</spine>
精彩评论