Xslt sum of absolute values

Discussion in 'XML' started by Juho Jussila, Nov 12, 2007.

  1. Juho Jussila

    Juho Jussila Guest

    Hi

    I'm trying to calculate sum of absolute values using Xslt 1.0
    and math:abs (http://www.exslt.org/math/functions/abs/index.html)

    I thought this would work:
    <xsl:value-of select="sum(math:abs(/A/B/C))"/>
    Well it doesn't, because there are multiple C's.

    Is there any nice trick how to do this, or do I have to use
    for-each -loop?

    Thanks in advance

    --
    // Juho Jussila
    \X/
     
    Juho Jussila, Nov 12, 2007
    #1
    1. Advertising

  2. Juho Jussila

    Pavel Lepin Guest

    Juho Jussila <> wrote in
    <>:
    > I'm trying to calculate sum of absolute values using Xslt
    > 1.0 and math:abs
    >
    > I thought this would work:
    > <xsl:value-of select="sum(math:abs(/A/B/C))"/>
    > Well it doesn't, because there are multiple C's.
    >
    > Is there any nice trick how to do this,


    None that I know of. sum() wants a nodeset. math:abs() only
    works with numbers.

    > or do I have to use for-each -loop?


    xsl:for-each is not a loop and won't help you one bit. You
    cannot carry your state (accumulator-to-date or
    whatchamacallit) over to the next iteration - because,
    effectively, there are no iterations. No guaranteed
    order-of-execution. No side effects. Stateless. Keep that
    in mind. Always.

    What you want is a named templated template that would
    recursively abs-sum your nodeset. Use either divide &
    conquer, which would mean (*screeching sounds of trying to
    remember what little of computational complexity theory I
    ever knew*) log N-deep recursion, which is just fine for
    most applications, or tail recursion (if your processor is
    capable of optimising it away - Dimitre Novatchev posted a
    link to his investigations of this matter some time ago).

    --
    ....also, I submit that we all must honourably commit seppuku
    right now rather than serve the Dark Side by producing the
    HTML 5 spec.
     
    Pavel Lepin, Nov 12, 2007
    #2
    1. Advertising

  3. Juho Jussila wrote:
    > Hi
    >
    > I'm trying to calculate sum of absolute values using Xslt 1.0
    > and math:abs (http://www.exslt.org/math/functions/abs/index.html)
    >
    > I thought this would work:
    > <xsl:value-of select="sum(math:abs(/A/B/C))"/>
    > Well it doesn't, because there are multiple C's.
    >
    > Is there any nice trick how to do this, or do I have to use
    > for-each -loop?
    >
    > Thanks in advance
    >


    sum(/A/B/C[. &gt; 0]) - sum(/A/B/C[. &lt; 0])

    David

    --
    http://dpcarlisle.blogspot.com
     
    David Carlisle, Nov 12, 2007
    #3
  4. Juho Jussila

    Pavel Lepin Guest

    David Carlisle <> wrote in
    <fhalu6$q1t$1$>:
    > Juho Jussila wrote:
    >> I'm trying to calculate sum of absolute values using Xslt
    >> 1.0 and math:abs
    >> (http://www.exslt.org/math/functions/abs/index.html)
    >>
    >> I thought this would work:
    >> <xsl:value-of select="sum(math:abs(/A/B/C))"/>
    >> Well it doesn't, because there are multiple C's.
    >>
    >> Is there any nice trick how to do this, or do I have to
    >> use for-each -loop?

    >
    > sum(/A/B/C[. &gt; 0]) - sum(/A/B/C[. &lt; 0])


    Ah, cuspy.

    --
    ....also, I submit that we all must honourably commit seppuku
    right now rather than serve the Dark Side by producing the
    HTML 5 spec.
     
    Pavel Lepin, Nov 13, 2007
    #4
  5. Juho Jussila

    Juho Jussila Guest

    David Carlisle <> writes:

    > sum(/A/B/C[. &gt; 0]) - sum(/A/B/C[. &lt; 0])


    Nice solution, thanks.

    --
    // Juho Jussila
    \X/ 050 358 0895
     
    Juho Jussila, Nov 13, 2007
    #5
    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. Benjamin Hillsley
    Replies:
    3
    Views:
    1,732
    Dimitre Novatchev
    Sep 25, 2003
  2. Rolf Kemper

    sum values by lookup

    Rolf Kemper, Dec 23, 2004, in forum: XML
    Replies:
    2
    Views:
    662
    Luke Dalessandro
    Dec 23, 2004
  3. equinox1248
    Replies:
    2
    Views:
    328
    Walter Roberson
    May 3, 2006
  4. James Byrne
    Replies:
    3
    Views:
    600
    James Byrne
    Sep 14, 2010
  5. Replies:
    2
    Views:
    148
Loading...

Share This Page