XSLT 1 兼容代码,用于在存在父值匹配时获取元素的总和 [英] XSLT 1 compliant code to get SUM of elements when there is a parent value match

查看:12
本文介绍了XSLT 1 兼容代码,用于在存在父值匹配时获取元素的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种符合 XSLT 1/1.1 的解决方案来对 XSLT 中的值求和.

I'm looking for an XSLT 1/1.1 compliant solution to summing values in XSLT.

采用以下 XML:

<Root>
    <Product>
        <DataState>Unchanged</DataState>
            <SortOrder>0</SortOrder>
            <ProductName>Car</ProductName>
            <Sales>
                <Amount>400.00</Amount>
        </Sales>
    </Product>
    <Product>
        <DataState>Unchanged</DataState>
            <SortOrder>0</SortOrder>
            <ProductName>Car</ProductName>
            <Sales>
                <Amount>700.00</Amount>
        </Sales>
    </Product>
    <Product>
        <DataState>Unchanged</DataState>
            <SortOrder>0</SortOrder>
            <ProductName>Car</ProductName>
            <Sales>
                <Amount>100.00</Amount>
        </Sales>
    </Product>
    <Product>
        <DataState>Unchanged</DataState>
            <SortOrder>0</SortOrder>
            <ProductName>Boat</ProductName>
            <Sales>
                <Amount>400.00</Amount>
        </Sales>
    </Product>
    <Product>
        <DataState>Unchanged</DataState>
            <SortOrder>0</SortOrder>
            <ProductName>Boat</ProductName>
            <Sales>
                <Amount>200.00</Amount>
        </Sales>
    </Product>
    <Product>
        <DataState>Unchanged</DataState>
            <SortOrder>0</SortOrder>
            <ProductName>Car</ProductName>
            <Sales>
                <Amount>400.00</Amount>
        </Sales>
    </Product>
</Root>

现在,很容易使用 XSLT 获取 XML 中的 Amount 总和并使用该值.我会使用这样的东西,返回的值是 2200.00.

Now, it's easy to use XSLT to get the sum of Amount in the XML and use that value. I would use something like this, and the value returned would be 2200.00.

<xsl:value-of select='sum(//Root/Product/Sales/Amount)'/>

但是,我需要做的是编写 XSLT 以获取来自 Boat 和 Car 的 Total Amount.

However, what I need to do is write XSLT to get Total Amount from Boat and from Car.

例如:对于汽车,返回的值为 1600.00.对于船,该值为 600.00.

For example: In the case of car, the value returned would be 1600.00. For boat, the value would be 600.00.

我将如何编写 2 个不同的 XSLT 命令来获取 Boat 和 Car 的总和?

How would I write 2 distinct XSLT commands to get the sum for Boat and for Car?

理论上我想我会是这样的,但我不认为它这么简单?

In theory I envision i would be something like this, but I don't imagine it's this simplistic?

<xsl:value-of select='sum(//Root/Product[ProductName=Boat]/Sales/Amount)'/>

<xsl:value-of select='sum(//Root/Product[ProductName=Car]/Sales/Amount)'/>

推荐答案

我没有想象一下就这么简单?

I don't imagine it's this simplistic?

差不多 - 除了您需要引用文字文本,并且您可能不需要 // 部分(这取决于您当前的上下文).例如:

Pretty much so - except you need to quote the literal text, and you probably don't need the // part (this depends on your current context). For example:

<xsl:template match="/">
    <amount type="boat">
        <xsl:value-of select='sum(Root/Product[ProductName="Boat"]/Sales/Amount)'/>
    </amount>
</xsl:template>

会回来:

<?xml version="1.0" encoding="UTF-8"?>
<amount type="boat">600</amount>

使用您的示例输入.

这篇关于XSLT 1 兼容代码,用于在存在父值匹配时获取元素的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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