我认为最好使用xsl:number
. 使用xsl:number
,您应该能够更轻松地排除元素。
这是一个小示例,显示了两者的结果position()
并xsl:number
用于比较。
XML 输入
<cities>
<city>City One</city>
<city>City Two</city>
<city>City Three</city>
<city>City Four</city>
<city>City Five</city>
<city>City Six</city>
<city>City Seven</city>
<city>City Eight</city>
<city>City Nine</city>
<city>City Ten</city>
</cities>
XSLT 2.0(我认为 2.0 是安全的,因为问题被标记为 saxon 并且您在评论中使用了 tokenize 。)
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="exclude" select="('City Three','City Six','City Nine')"/>
<xsl:template match="city[not(.=$exclude)]">
<city position="{position()}">
<xsl:attribute name="number">
<xsl:number count="city[not(.=$exclude)]"/>
</xsl:attribute>
<xsl:value-of select="."/>
</city>
</xsl:template>
<xsl:template match="city"/>
</xsl:stylesheet>
输出
<cities>
<city position="1" number="1">City One</city>
<city position="2" number="2">City Two</city>
<city position="4" number="3">City Four</city>
<city position="5" number="4">City Five</city>
<city position="7" number="5">City Seven</city>
<city position="8" number="6">City Eight</city>
<city position="10" number="7">City Ten</city>
</cities>