有一个列表
<nodes>
<node attr='1'/>
<node attr='0'/>
<node attr='1'/>
<node attr='1'/>
</nodes>我需要应用模板所有节点并对其进行计数:
<xsl:apply-templates select='nodes/node'>
<xsl:if test='@attr=1'>
<xsl:number/>
</xsl:if>
</xsl:apply-templates>但结果不是123,结果是134。如何在xslt-1.0中修复它?还有另一种方法可以给它设置数字吗?position()没有帮助,并且
<xsl:apply-templates select='nodes/node[@attr=1]'>
<xsl:if test='@attr=1'>
<xsl:number/>
</xsl:if>
</xsl:apply-templates>无助于=((
发布于 2012-06-29 16:34:31
首先,您的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:,并将代码放在其中...
<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计数属性来只计算您想要的元素的数量
<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
发布于 2012-06-29 16:14:41
上面写着123 -这是你要找的东西吗?
<xsl:for-each select="nodes/node[@attr='1']">
<xsl:value-of select="position()"/>
</xsl:for-each>发布于 2012-06-29 16:14:56
现在还不太清楚你想要实现什么。我假设您需要计算属性设置为1的节点的数量。在这种情况下,使用count函数:
<xsl:value-of select="count(nodes/node[@attr='1'])" />如果您需要输出与条件匹配的子集内所需节点的位置,则for-each可能是最佳选择:
<xsl:for-each select="nodes/node[@attr='1']">
<xsl:value-of select="position()" />
</xsl:for-each>https://stackoverflow.com/questions/11258245
复制相似问题