xsl sum siblings based on node value

為{幸葍}努か 提交于 2019-12-23 12:16:13

问题


I need to apply an xsl transformation that sums certain node values based on the value of one of its sibling nodes. Here's my xml pseudo code:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<Message>
    <Body>
        <Order>
            <Item>
                <.../>
                <Type>widget</Type>
                <Qty>20</Qty>
                <.../>
            </Item>
            <Item>
                <.../>
                <Type>gadget</Type>
                <Qty>10</Qty>
                <.../>
            </Item>
            <Item>
                <.../>
                <Type>widget</Type>
                <Qty>5</Qty>
                <.../>
            </Item>
            <Item/>
        </Order>
    </Body>
</Message>

The desired result of the the quantity summation for all items of type 'widget' is 25

<xsl:value-of select="sum(/Message/Body/Order/Item/Qty)"/>

is yielding 35

My xsl stylesheet details

xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
version="2.0"

回答1:


Try to add predicates to find the nodes that you're interested (i.e. 'widget') only:

<xsl:value-of select="sum(/Message/Body/Order/Item[Type = 'widget']/Qty)"/>


来源:https://stackoverflow.com/questions/17848008/xsl-sum-siblings-based-on-node-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!