Re: [xsl] Grouping by key

Subject: Re: [xsl] Grouping by key
From: Geert Josten <Geert.Josten@xxxxxxxxxxx>
Date: Fri, 12 Nov 2004 14:15:02 +0100
I think a key is easiest then...

<xsl:key name="ordervalues-by-town" match="*[@colno = 3]" use="../*[@colno =2]" />

Though it seems the following might work just as well:

<xsl:key name="ordervalues-by-town" match="ordervalue" use="../town" />

And when you want to insert the sum for a town, use:

    <xsl:value-of select="sum(key('ordervalues-by-town', *[@colno = 2]))" />
or
    <xsl:value-of select="sum(key('ordervalues-by-town', town))" />

Grtz,
Geert

Hi,

Geert wrote:

Are you trying to display the sum of all colno 3 elements? try

'sum(//*[@colno=3])' instead.


If you want the cumulative of the 'above', you could try '*[@colno = 3]

+ sum(preceding-sibling::account/*[@colno=3])'..


Thanks Geert. Your suggestions work but what I want to do is sum up that column values matching the key. Like when I am grouping the documents by Town, I want to sum up the 3rd column for that town. Please check my xml document format and required output format.

Asssuming my xml document is

<documents>
<account>
<accountnumber colno="0">0001</accountnumber>
<companyname colno="1">ABC Ltd</companyname>
<town colno="2">Ford</town>
<ordervalue colno="3">115.49</ordervalue>
<postcode colno="4">FD13QG</postcode>
</account>
<account>
<accountnumber colno="0">0002</accountnumber>
<companyname colno="1">XYZ Ltd</companyname>
<town colno="2">Ford</town>
<ordervalue colno="3">120.49</ordervalue>
<postcode colno="4">XY13QG</postcode>
</account>
<account>
<accountnumber colno="0">0003</accountnumber>
<companyname colno="1">HAM Ltd</companyname>
<town colno="2">Hamtown</town>
<ordervalue colno="3">110.34</ordervalue>
<postcode colno="4">HA13QG</postcode>
</account>
</documents>

I want an output like

Ford 235.98 0001 ABC Ltd 115.49 FD13QG
0002 XYZ Ltd 120.49 XY13QG


Hamtown                 110.34
        0003    HAM Ltd 110.34  HA13QG

Thanks.


--
Geert.Josten@xxxxxxxxxxx
IT-consultant at Daidalos BV, Zoetermeer (NL)

http://www.daidalos.nl/
tel:+31-(0)79-3316961
fax:+31-(0)79-3316464

GPG: 1024D/12DEBB50

Current Thread