【问题标题】:xsl How do I total the sum of one particular type of element based on an attribute from a different elementxsl如何根据来自不同元素的属性汇总一种特定类型元素的总和
【发布时间】:2016-07-14 21:14:46
【问题描述】:

给定以下 xml:

<orders>
    <order>
        <productName quantity="10">Blender</productName>
        <total price="20">200</total>
    </order>
    <order>
        <productName quantity="2">Computer</productName>
        <total price="1000">2000</total>
    </order>
    <order>
        <productName quantity="3">Scanner</productName>
        <total price="800">2400</total>
    </order>
</orders>

我想计算数量大于2的所有订单的总和。经过几个小时的尝试,我终于放弃了。这是我想出的解决方案之一(当然不行,返回0):

xsl:value-of select="sum(order/productName[@quantity>2]/total)"

请帮忙!

【问题讨论】:

    标签: xslt xpath


    【解决方案1】:

    使用sum(order[productName[@quantity&gt;2]]/total)

    【讨论】:

    • 非常感谢您的回复,马丁。有效。但是,我必须在 order 之前加上 '//',如:“sum(//order[productName[@quantity>2]]/total)” 才能正常工作。很高兴知道为什么必须这样做。我的意思是,我认为只有节点名就可以解决问题,但显然并非如此。
    • 嗯,您的原始代码 sn-p 没有显示任何上下文,但对于一个简单直接的 sum(order....),正如您所拥有的那样,我假设了一个匹配的模板 &lt;xsl:template match="orders"&gt;&lt;xsl:value-of select="sum(order[productName[@quantity&gt;2]]/total)"/&gt;&lt;/xsl:template&gt;
    猜你喜欢
    • 2012-10-01
    • 2022-10-04
    • 2011-07-18
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多