# Xslt sum of absolute values

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

1. ### Juho JussilaGuest

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?

--
// Juho Jussila
\X/

Juho Jussila, Nov 12, 2007

2. ### Pavel LepinGuest

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

3. ### David CarlisleGuest

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?
>
>

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

David

--
http://dpcarlisle.blogspot.com

David Carlisle, Nov 12, 2007
4. ### Pavel LepinGuest

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
5. ### Juho JussilaGuest

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