首先,您的 XSLT 中有错误
<xsl:apply-templates select='nodes/node'>
<xsl:if test='@attr=1'> <xsl:number/>
</xsl:if>
</xsl:apply-templates>
您不能在xsl:apply-templates中有xsl:if。您需要一个匹配的xsl:template并将代码放在那里...
<xsl:apply-templates select="nodes/node" />
<xsl:template match="node">
<xsl:if test='@attr=1'>
<xsl:number/>
</xsl:if>
<xsl:template>
实际上,您可以在此处取消xsl:if,只需让模板中的测试匹配
<xsl:template match="node[@attr=1]">
<xsl:number/>
<xsl:template>
但是要回答您的问题,您可能需要使用xsl:number元素上的count属性来仅计算您想要的元素
<xsl:number count="node[@attr=1]"/>
这是完整的 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="nodes/node"/>
</xsl:template>
<xsl:template match="node[@attr=1]">
<xsl:number count="node[@attr=1]"/>
</xsl:template>
<xsl:template match="node"/>
</xsl:stylesheet>
应用于您的 XML 时,结果为 123