我有一组带有属性的标签,我只想在匹配属性后将某些值拉入新组。
输入文件如下所示:
<content>
<manifest>
<item id="id1272682" href="ch01.html"/>
<item id="id1272759" href="ch02.html"/>
</manifest>
<spine>
<itemref idref="id1272759"/>
<itemref idref="id1273380"/>
</spine>
</content>
我希望我的XSLT在脊柱中查找与清单中的item / @ id匹配的任何itemref / @ idref值(可以是多个)并将该值添加到脊椎,以便输出看起来像这样:
<spine>
<itemref idref="id1272759"/>
</spine>
这是我到目前为止所拥有的。看来我的if:语句正在工作,因为我得到了正确数量的itemref标签,但没有得到idref的值。
<xsl:if test="itemref[attribute::idref = ../../manifest/item/@id]">
<xsl:element name="itemref">
<xsl:attribute name="idref">
<xsl:value-of select="@idref"/>
</xsl:attribute>
</xsl:element>
</xsl:if>
我的输出是:
<spine>
<itemref idref=""/>
</spine>
答案 0 :(得分:0)
此转化:
<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>
应用于提供的XML文档:
<content>
<manifest>
<item id="id1272682" href="ch01.html"/>
<item id="id1272759" href="ch02.html"/>
</manifest>
<spine>
<itemref idref="id1272759"/>
<itemref idref="id1273380"/>
</spine>
</content>
生成想要的正确结果:
<spine>
<itemref idref="id1272759"/>
</spine>