Combining Predicates In XPaths
I have some XML like this:
<MT N="tag1" V="text"/>
<MT N="tag2" V="more text"/>
<MT N="tag3" V="other text"/>
<MT N="tag4" V="something cool"/>
<MT N="target_tag" V="i want this"/>
<MT N="target_tag" V="and this"/>
I'm trying to target the MT
where N=target_tag
and by number (1 or 2).
The following doesn't work开发者_Python百科, despite what I was hoping from this link:
<xsl:variable name="display" select="MT[@N = 'target_tag' and 2]/@V" />
If I try this, I consistently get the first one:
<xsl:variable name="display" select="MT[@N = 'target_tag']/@V" />
And if I try this, I consistently get the second MT tag (so "more text" in this example):
<xsl:variable name="display" select="MT[2]/@V" />
I've tried this with no luck too:
<xsl:variable name="display" select="MT[2][@N = 'target_tag']/@V" />
Based on my requirements, I need to combine them, so that when I'm looping through a recursive function I can show the first, then the second, then the third.
Any ideas how these can be combined?
select="MT[@N = 'target_tag'][2]"
should work.
Input XML:
<root>
<MT N="tag1" V="text"/>
<MT N="tag2" V="more text"/>
<MT N="tag3" V="other text"/>
<MT N="tag4" V="something cool"/>
<MT N="target_tag" V="i want this"/>
<MT N="target_tag" V="and this"/>
</root>
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<xsl:for-each select="MT">
<xsl:if test="@N = 'target_tag'">
<xsl:value-of select="@V"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
i want thisand this
精彩评论