Straightforward way to sum calculated values across multiple parents?

Discussion in 'XML' started by The alMIGHTY N, Jan 18, 2007.

  1. <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
    The alMIGHTY N, Jan 18, 2007
    #1
    1. Advertising

  2. The alMIGHTY N

    Joris Gillis Guest

    Tempore 17:12:06, die Thursday 18 January 2007 AD, hinc in foro {comp.text.xml} scripsit 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?


    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,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    «Quot capita, tot sententiae» - Terentius , Phormio 454
    Joris Gillis, Feb 9, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Homam
    Replies:
    1
    Views:
    357
    Homam
    Mar 4, 2005
  2. Vasileios Zografos
    Replies:
    5
    Views:
    439
    Vasileios Zografos
    Jul 1, 2003
  3. news.microsoft.com
    Replies:
    2
    Views:
    419
    Alan Silver
    Jul 22, 2007
  4. Ramon F Herrera
    Replies:
    11
    Views:
    1,349
    Joe Kesselman
    Jun 7, 2012
  5. taj sha
    Replies:
    0
    Views:
    686
    taj sha
    Aug 14, 2012
Loading...

Share This Page