XSLT 1 compliant code to get SUM of elements when there is a parent value match

久未见 提交于 2019-12-12 02:27:38

问题


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

Take the following 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>

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

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

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

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

回答1:


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>

would return:

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

using your example input.



来源:https://stackoverflow.com/questions/23897188/xslt-1-compliant-code-to-get-sum-of-elements-when-there-is-a-parent-value-match

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