Home > Net >  XSLT 1.0 : sum of math calculations of sub-elements
XSLT 1.0 : sum of math calculations of sub-elements

Time:11-25

I would need to calculate total value of items in parcels (sum) for the shipment using data from sub-elements. I'm trying to get around a problem in source system by doing some calculations with XSLT (version 1.0 only available). The issue looks like this:

<Shipment>
...
    <Parcels>
        <Parcel>
            <Lenght>10</Lenght>
            <Width>20</Width>
            <Height>30</Height>
            <Weight>10</Weight>
            <Products>
                <Product>
                    <ItemNo>12345</ItemNo>
                    <Description>Shirt</Description>
                    <OrderedQty>4</OrderedQty>
                    <DeliveredQty>2</DeliveredQty>
                    <LineValue>400</LineValue>
                    <Currency>EUR</Currency>
                </Product>
                <Product>
                    <ItemNo>54321</ItemNo>
                    <Description>Trousers</Description>
                    <OrderedQty>1</OrderedQty>
                    <DeliveredQty>1</DeliveredQty>
                    <LineValue>50</LineValue>
                    <Currency>EUR</Currency>
                </Product>
            </Products>
        </Parcel>
        <Parcel>
            <Lenght>15</Lenght>
            <Width>25</Width>
            <Height>35</Height>
            <Weight>5</Weight>
            <Products>
                <Product>
                    <ItemNo>23456</ItemNo>
                    <Description>Jacket</Description>
                    <OrderedQty>2</OrderedQty>
                    <DeliveredQty>1</DeliveredQty>
                    <LineValue>300</LineValue>
                    <Currency>EUR</Currency>
                </Product>
            </Products>
        </Parcel>
    </Parcels>
</Shipment>

System, that generates item info per parcel, will calculate LineValue incorrectly, when there's a partial delivery. In the example XML above the item 12345 (Shirt) in the first parcel has an UnitValue of 100, and the system generates LineValue of 400 (OrderedQty * UnitValue). However, only 2/4 items were delivered, so the correct LineValue should have been DeliveredQty * 100 = 200. Since I don't have the real unit value in the XML, there's a need for some calculations within each Product - element. this I can do with some variables:

<xsl:for-each select="Products/Product">
    <xsl:variable name="OrderedQty">
        <xsl:value-of select="OrderedQty"/>
    </xsl:variable>
    <xsl:variable name="DeliveredQty">
        <xsl:value-of select="DeliveredQty"/>
    </xsl:variable>
    <xsl:variable name="LineValue">
        <xsl:value-of select="LineValue"/>
    </xsl:variable>
    <xsl:variable name="UnitValue">
        <xsl:value-of select="$LineValue div $OrderQty"/>
    </xsl:variable>
    <xsl:variable name="RealLineValue">
        <xsl:value-of select="$DeliveredQty * $UnitValue"/>
    </xsl:variable>
</xsl:for-each>

So I get correct unit value and new calculated line value for each product OK... BUT... (here comes the real challenge) :

How can I do the sum calculation into the Shipment - level by summing those calculated $RealLineValues from each Product-element together?

I tried with recursive template I found from StackOverflow:

<xsl:template name="sum">
        <xsl:param name="nodes"/>
        <xsl:param name="sum" select="0"/>
        <xsl:variable name="current" select="$nodes[1]"/>
        <xsl:if test="$current">
            <xsl:call-template name="sum">
                <xsl:with-param name="nodes" select="$nodes[position() &gt; 1]"/>
                <xsl:with-param name="sum" select="$sum   $current/$RealLineValue"/>
            </xsl:call-template>
        </xsl:if>
        <xsl:if test="not($current)">
            <xsl:value-of select="$sum"/>
        </xsl:if>
    </xsl:template>

And by calling that template from upper level, but obviously the variable $RealLineValue cannot be used like that.

As you might see from my trials, I'm not that good with XSLT, so any help id greatly appreciated :)

If you can provide the solution to calculate "sum(LineValue div OrderedQty) * DeliveredQty)" over each product in each parcel into the shipment level, using XSLT 1.0 , that would be SUPER!

And yes, the result should be 2*(400/4) 1(50/1) 1(300/2) = 200 50 150 = 400

HUGE thanks in advance.

CodePudding user response:

If you want to use a recursive template, you can do:

XSLT 1.0

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

<xsl:template match="/Shipment">
    <sum>
        <xsl:call-template name="sum">
            <xsl:with-param name="nodes" select="//Product"/>
        </xsl:call-template>
    </sum>
</xsl:template>

<xsl:template name="sum">
    <xsl:param name="nodes"/>
    <xsl:param name="sum" select="0"/>
    <xsl:variable name="current" select="$nodes[1]"/>
    <xsl:choose>
        <xsl:when test="$current">
            <xsl:variable name="UnitValue" select="$current/LineValue div $current/OrderedQty"/>
            <xsl:variable name="RealLineValue" select="$current/DeliveredQty * $UnitValue"/>
            <xsl:call-template name="sum">
                <xsl:with-param name="nodes" select="$nodes[position() > 1]"/>
                <xsl:with-param name="sum" select="$sum   $RealLineValue"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$sum"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

For an alternative approach see: XSL how to calculate the sum of the product of the attribute values of each element

  • Related