【问题标题】:How to get the sum of the values from different nodes in XML file using XSLT如何使用 XSLT 从 XML 文件中的不同节点获取值的总和
【发布时间】:2013-10-16 09:35:19
【问题描述】:

我是 XSLT 的新手,不知道如何处理它。 我的 XML 文件是:

<?xml version="1.0"?>
<books>
  <book>
    <title type="fiction">CS115</title>
    <authors>Adams</authors>
    <price>12.0</price>
  </book>
  <book>
    <title type="fiction">CS205</title>
    <authors>Dykes</authors>
    <price>50.2</price>
  </book>
  <book>
    <title type="nonfiction">CS255</title>
    <authors>Brunner,Computer</authors>
    <price>11.5</price>
  </book>
   <book>
    <title type="nonfiction">CS118</title>
    <authors>Adams,Bououss</authors>    
    <price>20.0</price>
  </book>
   <book>
    <title type="fiction">The Hobbit</title>
    <authors>J.R.R. Tolkien</authors>    
    <price>25.99</price>
  </book>
</books>

我想计算价格低于 30 且 title/@type 为小说的书籍的总价。

【问题讨论】:

    标签: xml xslt xpath xslt-2.0


    【解决方案1】:

    从学习一些 XPath 开始:/books/book 选择所有 book 元素,/books/book[title/@type = 'fiction'] 标题类型属性为虚构的元素,/books/book[title/@type = 'fiction' and price &lt; 30] 添加有关价格的约束,最后 sum(/books/book[title/@type = 'fiction' and price &lt; 30]/price) 计算总和。

    在 XSLT 代码内部,您需要转义 &lt;&lt;xsl:value-of select="sum(/books/book[title/@type = 'fiction' and price &amp;lt; 30]/price)"/&gt;

    【讨论】:

    • 天啊,这么简单吗?我尝试了一些方法,比如定义一个地球变量,然后添加价格,但似乎 xslt 不允许这样做......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多