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

Subject: RE: [xsl] Not-repeated elements in an <xsl:for-each>
From: "Kenny Akridge" <kakridge@xxxxxxxxxxxxx>
Date: Sun, 21 Dec 2003 19:25:29 -0500
I had to do something similar to this, but I approached it a little
differently.  I would first sort based on type.  Then I would loop
through all of the ships.  I would have some logic to check if following
ship type = context ship type(".").  If they are different, I would then
apply a key that matches your ship type and take the sum of those
values.

<xsl:key name="subTotal" match="root/ship" use="@type"/>

<xsl:template match="root">
   <xsl:for-each select="ship">
     <xsl:sort select="ship/@type"/> <--- check syntax
     <xsl:variable name="test" select="following::ship/@type"/>
	<xsl:choose>
	 <xsl:when test="not(./@type = following::ship/@type)">
	  <xsl:value-of select="format-number(sum(key('subTotal',
ship/@type) , '#.00')"/>
       </xsl:when>
     </xsl:choose>
   </xsl:for-each>
</xsl:template>

I wasn't using attributes, so I'm not certain if this will work with
attributes.  I hope it helps though.

-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Caranthir
Morifinwë
Sent: Sunday, December 21, 2003 6:48 PM
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] 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