This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: sort and sum
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: "Maneshi Tuli" <maneshi dot tuli at db dot com>
- Cc: xsl-list at lists dot mulberrytech dot com
- Date: Fri, 30 Nov 2001 17:24:06 +0000
- Subject: Re: [xsl] sort and sum
- Organization: Jeni Tennison Consulting Ltd
- References: <OFA2AF50B6.4E844D43-ON85256B14.005D0D7F@db.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi Maneshi,
> i want to get the sum for elements after 10 top elements
The easiest way to do this is with a two-step transformation. First
sort the rows into the order that they should be in:
<xsl:variable name="sorted-rows-rtf">
<xsl:for-each select="row">
<xsl:sort select="col" data-type="number" order="descending" />
<xsl:copy-of select="." />
</xsl:for-each>
</xsl:variable>
Then convert that result tree fragment into a node set using a
node-set() extension function (which one you use depends on what
processor you're using):
<xsl:variable name="sorted-rows"
select="exsl:node-set($sorted-rows-rtf)/row" />
And then sum the top ten with the sum() function:
<xsl:value-of select="sum($sorted-rows[position() <= 10]/col)" />
If you don't want to use an extension function, then the 'pure' way is
either using two stylesheets or a recursive template that steps
through the rows one by one, keeping track of their sum as you go. Let
us know if you need help putting that together.
I think that Dimitre also has a generic template for doing sorting and
summing...
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list