XSLT sum函数

时间:2018-09-13 14:00:38

标签: xml xslt

我需要生成一个项,该项将为给定的XML输入累计总计。

XML输入为:

<item id="15117180817160" amount="1441.63"/>
<item id="15117180817161" amount="300.10"/>        

这是我的XSLT:

<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
 <tr>
   <th scope="col">Nro. de Reclamo</th>
   <th scope="col">Monto $</th>
 </tr>                   
 <xsl:for-each select="item">                
    <tr>
        <td align="left">
          <xsl:value-of select="@item" />
        </td>
        <td align="center" style="width:8%;">
          <xsl:value-of select="@amount" />
        </td>
   </tr>
 </xsl:for-each>
    <tr>
     <td></td>
     <td></td>
     <td align="right">
      Total:
     </td>
     <td>
      <xsl:value-of select="sum(/item/@amount)"/>
     </td>
     </tr>
 </table>

这是我的输出:

<html xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
<body>
<h2></h2>
<table border="1">
 <tr bgcolor="#9acd32">
    <th>id</th>
    <th>amount</th>
 </tr>
 <tr>
    <td>15117180817160</td>
    <td>1441.63</td>
    <br/>
</tr>
 <tr>
    <td>15117180817161</td>
    <td>300.10</td>
    <br/>
</tr>
Total: 0
</table>

结果应为Total = 1741,73

如何实现求和函数以返回正确的总值?

最佳问候 豪尔赫

1 个答案:

答案 0 :(得分:0)

而不是这样做...

 <xsl:value-of select="sum(/item/@amount)"/>

执行此操作...

 <xsl:value-of select="sum(item/@amount)"/>

第一个表达式中的开头/代表文档节点,因此只有在子根元素称为item时它才起作用。您没有在XML示例中显示它,但是由于您有多个item元素,因此必须将它们包含在父元素中。