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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top