XSLT grouping and summing

主宰稳场 提交于 2019-12-20 07:35:52

问题


I am new to XSLT and need to sum the total price (Quantity * UnitPrice) of items based on ID from each orders, and print it at the end of each Item's group using XSLT 1.0. Here is my example XML

<Orders>
<Order>
    <Reference>234</Reference>
    <Item>
        <ID>10</ID>
        <Quantity>1</Quantity>
        <UnitPrice>2</UnitPrice>
    </Item>
    <Item>
        <ID>10</ID>
        <Quantity>2</Quantity>
        <UnitPrice>3</UnitPrice>
    </Item>
    <Item>
        <ID>10</ID>
        <Quantity>2</Quantity>
        <UnitPrice>2</UnitPrice>
    </Item>
    <Item>
        <ID>20</ID>
        <Quantity>2</Quantity>
        <UnitPrice>4</UnitPrice>
    </Item>
</Order>
<Order>
    <Reference>456</Reference>
    <Item>
        <ID>10</ID>
        <Quantity>2</Quantity>
        <UnitPrice>2</UnitPrice>
    </Item>
    <Item>
        <ID>20</ID>
        <Quantity>2</Quantity>
        <UnitPrice>2</UnitPrice>
    </Item>
</Order>
</Orders>

The desired output XML is below

<SAPOrders>
<Order>
    <Reference>234</Reference>
    <Item>
        <Quantity>1</Quantity>
        <UnitPrice>2</UnitPrice>
    </Item>
    <Item>
        <Quantity>2</Quantity>
        <UnitPrice>3</UnitPrice>
    </Item>
    <Item>
        <Quantity>2</Quantity>
        <UnitPrice>2</UnitPrice>
        <Total>12</Notes>
    </Item>
    <Item>
        <Quantity>2</Quantity>
        <UnitPrice>4</UnitPrice>
        <Total>8</Notes>
    </Item>
</Order>
<Order>
    <Reference>456</Reference>
    <Item>
        <Quantity>2</Quantity>
        <UnitPrice>2</UnitPrice>
        <Total>4</Notes>
    </Item>
    <Item>
        <Quantity>2</Quantity>
        <UnitPrice>2</UnitPrice>
        <Total>4</Total>
    </Item>
</Order>
</SAPOrders>

回答1:


There are two problems here: (1) group the items by order and by item ID, and (2) calculate the subtotal for each such group.

The first problem is relatively easy and can be solved using the Muenchian grouping method. The second problem is more difficult, since XSLT 1.0 does not allow summing the result of a calculation.

The following stylesheet starts by pre-calculating the extended price of each item and storing the results in a variable. Then it operates on the variable, grouping the items and subtotaling each group.

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="item" match="Item" use="@key" />

<xsl:template match="/Orders">
    <!-- first pass -->
    <xsl:variable name="first-pass">
        <xsl:for-each select="Order">
            <Order>
                <xsl:copy-of select="Reference"/>
                <xsl:for-each select="Item">
                    <Item key="{concat(../Reference, '|', ID)}" extPrice="{Quantity * UnitPrice}">
                        <xsl:copy-of select="*"/>
                    </Item>
                </xsl:for-each>
            </Order>
        </xsl:for-each>
    </xsl:variable>
    <!-- output -->
    <SAPOrders>
        <xsl:for-each select="exsl:node-set($first-pass)/Order">
            <xsl:copy>
                <xsl:copy-of select="Reference"/>
                <!-- for each unique item in this order -->
                <xsl:for-each select="Item[count(. | key('item', @key)[1]) = 1]">
                    <!-- list the items in this group -->
                    <xsl:for-each select="key('item', @key)">
                        <Item>
                            <xsl:copy-of select="Quantity | UnitPrice"/>
                            <!-- add the subtotal of this group -->
                            <xsl:if test="position()=last()">
                                <Total>
                                    <xsl:value-of select="sum(key('item', @key)/@extPrice)" />
                                </Total>
                            </xsl:if>
                        </Item>
                    </xsl:for-each>
                </xsl:for-each>
            </xsl:copy>
        </xsl:for-each> 
    </SAPOrders>
</xsl:template>

</xsl:stylesheet>

Demo: http://xsltransform.net/jz1PuPz



来源:https://stackoverflow.com/questions/41549625/xslt-grouping-and-summing

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