Straightforward way to sum calculated values across multiple parents?

T

The alMIGHTY N

<store>
<frequent_shopper_discount value="5"/>
<premium_member_discount value="10"/>
<inventory>
<item>
<msrp value="3.99"/>
</item>
<item>
<msrp value="2.78"/>
</item>
<item>
<msrp value="16.24"/>
</item>
<item>
<msrp value="9.66"/>
</item>
<item>
<msrp value="4.53"/>
</item>
</inventory>
</store>

Is there a straightforward way to calculate the sum of these item's
MSRP values rounded at the item level? By this I mean to apply the 15%
discount on each of the individual MSRPs before summing them?

Right now, my report is outputting a row for each of the individual
items in the inventory. I do this in a template that matches the store
element by iterating through inventory/item and applying the round
function as follows:

round(msrp/@value * 100 * (1 -
(number(../../frequent_shopper_discount/@value) div 100)) * (1 -
(number(../../premium_member_discount/@value) div 100)))

I'm looking for a straightforward way to do this without having to
change the XML structure because any changes would require the Java
development staff to invest time that they do not have right now.

Thanks in advance for any help you can give.

Regards,

Nathaniel
 
J

Joris Gillis

Tempore 17:12:06 said:
<store>
<frequent_shopper_discount value="5"/>
<premium_member_discount value="10"/>
<inventory>
<item>
<msrp value="3.99"/>
</item>
<item>
<msrp value="2.78"/>
</item>
<item>
<msrp value="16.24"/>
</item>
<item>
<msrp value="9.66"/>
</item>
<item>
<msrp value="4.53"/>
</item>
</inventory>
</store>

Is there a straightforward way to calculate the sum of these item's
MSRP values rounded at the item level? By this I mean to apply the 15%
discount on each of the individual MSRPs before summing them?

Hi,


In XSLT2, this is a trivial task:

sum(for $item in inventory return round(msrp/@value ...))

In XSLT1, however it's a bit trickier. You might try to tackle it using recursion. For example:

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

<xsl:eek:utput indent="yes"/>

<xsl:template match="inventory">
<!-- Start summing with the first item-->
<xsl:apply-templates select="item[1]" mode="sumDiscount"/>
</xsl:template>

<xsl:template match="item" mode="sumDiscount">
<xsl:param name="runningSum" select="0"/>

<xsl:variable name="updatedSum" select="$runningSum + round(msrp/@value * 100 * (1 -
(number(../../frequent_shopper_discount/@value) div 100)) * (1 -
(number(../../premium_member_discount/@value) div 100))) div 100"/>

<!-- Proceed summing with the next item-->
<xsl:apply-templates select="following-sibling::item[1]" mode="sumDiscount">
<xsl:with-param name="runningSum" select="$updatedSum"/>
</xsl:apply-templates>

<!-- No more items. Summing is complete-->
<xsl:if test="not(following-sibling::item)">
<xsl:value-of select="$updatedSum"/>
</xsl:if>

</xsl:template>

</xsl:stylesheet>

regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,247
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top