XML - problem with sum function

Discussion in 'XML' started by cieron, May 16, 2005.

  1. cieron

    cieron Guest

    Hi
    I've got something like this one:

    <positions>
    <position>
    <value>1000</value>
    <count>3</count>
    </position>
    <position>
    <value>800</value>
    <count>2</count>
    </position>
    </positions>

    I need on my page this result

    1000 * 3 + 800 * 2 = 4600

    but sum(position/value * position/count) is not working. How can I get what
    I want?


    --

    Thanks,
    cieron
     
    cieron, May 16, 2005
    #1
    1. Advertising

  2. "cieron" <> writes:

    > Hi
    > I've got something like this one:
    >
    > <positions>
    > <position>
    > <value>1000</value>
    > <count>3</count>
    > </position>
    > <position>
    > <value>800</value>
    > <count>2</count>
    > </position>
    > </positions>
    >
    > I need on my page this result
    >
    > 1000 * 3 + 800 * 2 = 4600
    >
    > but sum(position/value * position/count) is not working. How can I get what
    > I want?
    >
    >
    > --
    >
    > Thanks,
    > cieron



    sum has to take a node set, so you can not in pure XSLT1 use sum() over
    a computed expression.

    IN Xpath2 (which is still in draft form, but implemented in saxon 8) you
    will be able to do
    sum(position/(value * count))

    However for XSLT1, possibly the simplest is to use yor processor's
    xx:node-set() extension function (if it has one, most do) and first
    generate a list of products


    <xsl:variable name="x">
    <xsl:for-each select="position"/>
    <x><xsl:value-of select="value * count"/></x>
    </xsl:for-each>
    </xsl:variable>

    then sum these new x elements.:

    <xsl:value-of select="xx:node-set($x/x)


    Or you need to write a recursive template that accumuates the sum

    <xsl:template match="positions">
    <xsl:apply-templates select="position[1]"/>
    </xsl:template>

    <xsl:template match="position">
    <xsl:param name="s" select="0"/>/>
    <xsl:variable name="s2" select="$s + value * count"/>
    <xsl:choose>
    <xsl:when test="following-sibling::position">
    <xsl:apply-templates select="following-sibling::position[1]">
    <xsl:with-param name="s" select="$s2"/>
    </xsl:apply-templates>
    <xsl:eek:therwise>
    <xsl:value-of select="$s2"/>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    David
     
    David Carlisle, May 16, 2005
    #2
    1. Advertising

  3. "cieron" <> wrote in message
    news:d6a3tk$cc1$...
    > Hi
    > I've got something like this one:
    >
    > <positions>
    > <position>
    > <value>1000</value>
    > <count>3</count>
    > </position>
    > <position>
    > <value>800</value>
    > <count>2</count>
    > </position>
    > </positions>
    >
    > I need on my page this result
    >
    > 1000 * 3 + 800 * 2 = 4600
    >
    > but sum(position/value * position/count) is not working. How can I get
    > what
    > I want?



    FXSL is your friend -- look for the foldl or runningTotals template.

    or directly sumProducts.xsl in:
    http://www.mulberrytech.com/Extreme/Proceedings/xslfo-pdf/2003/Novatchev01/EML2003Novatchev01.pdf


    Cheers,
    Dimitre Novatchev
     
    Dimitre Novatchev, May 16, 2005
    #3
    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. Bryce (Work)

    Grouping and Sum problem

    Bryce (Work), Sep 30, 2003, in forum: XML
    Replies:
    3
    Views:
    510
    Bryce (Work)
    Oct 1, 2003
  2. Replies:
    0
    Views:
    1,324
  3. Weng Tianxiang
    Replies:
    2
    Views:
    470
  4. sum function

    , Oct 4, 2012, in forum: Python
    Replies:
    26
    Views:
    476
    Ramchandra Apte
    Oct 6, 2012
  5. Russel Walker
    Replies:
    9
    Views:
    121
    Russel Walker
    Jul 7, 2013
Loading...

Share This Page