[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Not-repeated elements in an <xsl:for-each>

Subject: RE: Not-repeated elements in an <xsl:for-each>
From: "Lars Huttar" <lars_huttar@xxxxxxx>
Date: Sun, 21 Dec 2003 21:00:57 -0600
xsl for each sum
Dear Caranthir (by the way ),

This is a classic grouping problem.
Here's a short solution:

<xsl:template match="/root">
 <!-- Select the first ship element of each unique type. -->
 <!-- This assumes all ships are siblings. -->
 <xsl:for-each select="ship[not(preceding-sibling::ship/@type = @type)]">
   <xsl:variable name="stype" select="@type" />
   <xsl:value-of select="sum(../ship[@type = $stype])" />
 </xsl:for-each>
</xsl:template>

If you have a lot of ships, you could use a key (xsl:key) to make this
more efficient.
See the XSL FAQ (http://www.dpawson.co.uk/xsl/sect2/sect21.html I think)
under "Grouping" for more details. Specifically, Meunchian grouping.

Lars

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of Caranthir
> Morifinwë
> Sent: Sunday, December 21, 2003 5:48 PM
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  Not-repeated elements in an <xsl:for-each>
>
>
> I wonder if there is some way to get a list of nodes without repeated
> elements, something similitar to de "DISTINCT" clausule in a
> SELECT sentence
> of SQL. I need to do this because i want to show the total sum of many
> attributes from many elements. There is an example xml:
>
> <root>
>     <ship type="astropod" num="100"/>
>     <ship type="spider" num="150"/>
>     <ship type="astropod" num="50"/>
> </root>
>
> If I use the following XSL template:
>
> <xsl:template match="/root">
>     <xsl:for-each select=ship/@type>
>         <xsl:variable name="stype" select="."/>
>         <xsl:value-of select="sum(/root/ship[@type=stype]/@num)"/>
>     </xsl:for-each>
> </xsl:template>
>
> i get "150 150 150", repeating the third ship who has the
> same type than first. Is there any way to only get one
> "astropod" type in the list xsl:for-each iterates?
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.