Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Xslt sum of absolute values

Reply
Thread Tools

Xslt sum of absolute values

 
 
Juho Jussila
Guest
Posts: n/a
 
      11-12-2007
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/
 
Reply With Quote
 
 
 
 
Pavel Lepin
Guest
Posts: n/a
 
      11-12-2007

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.
 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      11-12-2007
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
 
Reply With Quote
 
Pavel Lepin
Guest
Posts: n/a
 
      11-13-2007

David Carlisle <david-> 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.
 
Reply With Quote
 
Juho Jussila
Guest
Posts: n/a
 
      11-13-2007
David Carlisle <david-> writes:

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


Nice solution, thanks.

--
// Juho Jussila
\X/ 050 358 0895
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How does one get an absolute absolute file path? James Byrne Ruby 3 09-14-2010 06:02 PM
Finding sum of absolute differences of memory blocks equinox1248 C Programming 2 05-03-2006 07:11 AM
Stylized text with absolute values vs. relative values and scalability on a Mac robbiehenry@gmail.com Javascript 2 09-19-2005 03:08 PM
sum values by lookup Rolf Kemper XML 2 12-23-2004 05:32 PM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57