XSLT 汇总值 [英] XSLT summarize value

查看:27
本文介绍了XSLT 汇总值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为给定的 XML 输入生成一个累加总数的项目.

I need to generate an item that accumulates a total for a given XML input.

XML 输入是:

<catalog xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
  <item id="1" amount="10" />
  <item id="2" amount="20" />           
</catalog>

这是我的 XSLT:

 <?xml version="1.0" encoding="ISO-8859-1"?>
  <xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
  <xsl:template match="/">
  <html>
 <body>
 <h2>My CD Collection</h2>
 <table border="1">
  <tr bgcolor="#9acd32">
    <th>Id</th>
    <th>Amount</th>     
  </tr>

  <xsl:for-each select="catalog/item">
  <tr>
    <td><xsl:value-of select="@id"/></td>
    <td><xsl:value-of select="@amount"/></td>   

  <br/>
   Total: 

  </tr>
  </xsl:for-each>

</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>1</td>
        <td>10</td>
        <br/>
 Total: 

  </tr>
     <tr>
        <td>2</td>
        <td>20</td>
        <br/>
  Total: 

  </tr>
  </table>

结果应该是 Total = 30

The result should be Total = 30

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

How can I implement a sum function that returns the correct total value?

推荐答案

使用 sum() 函数来合计 @amount 值:

Use the sum() function to total the @amount values:

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

请注意,您需要将 Total: 标签与上述 xsl:value-of outside 一起移动到 >xsl:for-each 循环.

Note that you'll want to move your Total: label along with the above xsl:value-of outside of your xsl:for-each loop.

这篇关于XSLT 汇总值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆