基于节点值的 xsl 和兄弟姐妹 [英] xsl sum siblings based on node value

查看:26
本文介绍了基于节点值的 xsl 和兄弟姐妹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要应用一个 xsl 转换,根据其同级节点之一的值对某些节点值求和.这是我的 xml 伪代码:

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>

'widget' 类型的所有项目的数量总和的期望结果是 25

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)"/>

正在产生 35

我的 xsl 样式表详细信息

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"

推荐答案

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

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)"/>

这篇关于基于节点值的 xsl 和兄弟姐妹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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