This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
xsl:sort by a sum()?
- From: "Juan Gril" <juangril at yahoo-inc dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Thu, 11 Apr 2002 17:03:28 -0300
- Subject: [xsl] xsl:sort by a sum()?
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hello everybody. Here is a question from a non programmer: I'm trying to
sort elements by a sum operation. Here is an XML structurally similar to the
one I'm using:
<FruitBasket location="kitchen">
<Fruit name="Apples" amount="10"/>
<Fruit name="Oranges" amount="20"/>
</FruitBasket>
<FruitBasket location="bedroom">
<Fruit name="Apples" amount="8"/>
<Fruit name="Oranges" amount="7"/>
</FruitBasket>
And the following <xsl:for-each>:
<xsl:for-each select="//Fruit[@name and not(@name=preceding::Fruit/@name)]">
<xsl:variable name="currentFruit" select="@name"/>
<xsl:value-of select="$currentFruit"><xsl:text> : </xsl:text><xsl:value-of
select="sum(//Fruit[@name=$currentFruit]/@amount)"/><br/>
</xsl:for-each>
After processing with Saxon 6.5.1 I get this:
Apples: 18
Oranges: 27
Which is fine and dandy, but I've got 25 FruitBaskets with different amount
of fruits each, so I need to sort them by the sum of each Fruit among
FruitBaskets. Is there an Xpath expression that could achieve that? I had
tried many, and also searched in the list, but I couldn't find something
similar to my case.
Many thanks in advance for the tip.
Cheers,
Juan
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list