我認爲使用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是安全的假設,因爲這個問題被標記撒克遜和你在註釋中使用的記號化。)
<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>
也許[這個答案](http://stackoverflow.com/questions/226405/find-position-of-a-node-using-xpath)會幫助你嗎? –