xsl基于节点值的sum兄弟

时间:2013-07-25 02:28:05

标签: xslt xslt-2.0

我需要应用xsl转换,该转换根据其兄弟节点之一的值对某些节点值求和。这是我的xml伪代码:

<?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>

“小部件”类型的所有项目的数量总和的期望结果是25

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

产生35

我的xsl样式表详细信息

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 个答案:

答案 0 :(得分:3)

尝试添加谓词以仅查找您感兴趣的节点(即“小部件”):

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