2012-03-14 101 views
0

我有以下xml。xslt循环添加数字

<employee> 
    <record id=1> 
     <fname>mark</fname> 
     <lname>smith</lname> 
     <id>10</id> 
    <record id=2> 
    ........ 
</employee> 

我想为每条记录添加id并获得总数。

我不知道我在雇员下有多少记录。它可能是1或10或100.

我发现以下示例来自论坛之一。我可以使用它,但他们是一个更简单的方法来实现这一目标?

<xsl:call-template name="for.loop"> 
<xsl:with-param name="i">1</xsl:with-param> 
<xsl:with-param name="count">10</xsl:with-param> 
</xsl:call-template> 
<!-- Rename "old name" elements to "new name" --> 
<xsl:template name="for.loop"> 
<xsl:param name="i"/> 
<xsl:param name="count"/> 
<xsl:if test="$i &lt;= $count"> 
<!-- body of the loop goes here --> 
</xsl:if> 
<xsl:if test="$i &lt;= $count"> 
    <xsl:call-template name="for.loop"> 
    <xsl:with-param name="i"> 
    <!-- Increment index--> 
    <xsl:value-of select="$i + 1"/> 
    </xsl:with-param> 
    <xsl:with-param name="count"> 
    <xsl:value-of select="$count"/> 
    </xsl:with-param> 
    </xsl:call-template> 
</xsl:if> 
</xsl:template> 
+1

没有很好地形成你的数据的XML(没有引号属性),而你还没有让你清楚是否要总结的ID属性或id元素。 – 2012-03-15 07:48:24

回答

1

有一个sum功能:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="/"> 
    <total> 
     <xsl:value-of select="sum(/employee/record/id)"/> 
    </total> 
    </xsl:template> 

</xsl:stylesheet>