XSL: SUM BY GROUP

R

Rivky

Hi. I have XML that is something like this?

<ROW>
<SYMBOL>A</SYMBOL>
<AMOUNT>10</AMOUNT>
</ROW>
<ROW>
<SYMBOL>A</SYMBOL>
<AMOUNT>20</AMOUNT>
</ROW>
<ROW>
<SYMBOL>B</SYMBOL>
<AMOUNT>10</AMOUNT>
</ROW>
<ROW>
<SYMBOL>C</SYMBOL>
<AMOUNT>50</AMOUNT>

</ROW>
<ROW>
<SYMBOL>C</SYMBOL>
<AMOUNT>60</AMOUNT>
</ROW>

I would like to get the SUM of AMOUNT based on the Symbol. Is there any
way to do this dynamically, using XSL/XSLT without having to specify
SYMBOL = 'A'. Rather something like For each Symbol, sum the Amounts.

Thanks in advance!
 
D

Dimitre Novatchev

First find the distinct values for SYMBOL, using a grouping method (such as
the Muenchian method,
see: http://www.jenitennison.com/xslt/grouping/)

Then iterate over these distinct values and produce the following value:

sum($yourDocument//ROW[SYMBOL = current()]/AMOUNT)

Of course, you may be using keys, in which case you'll be evaluating
something like this:

sum(key('kSymByVal', $aDistinctValue)/../AMOUNT)


Cheers,
Dimitre Novatchev
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,843
Latest member
WizcraftEntertainmentAgen

Latest Threads

Top