This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Counting unique node values using XSLT?
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: Re: [xsl] Counting unique node values using XSLT?
- From: "Thomas B. Passin" <tpassin at mitretek dot org>
- Date: Fri, 1 Jun 2001 18:05:36 -0400
- References: <23D1CA44E737D2119A6100A0C9D60B7DDD684D@sonusdc2.sonusnet.com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
This stylesheet works correctly on your example. The method is a simplified
version of the so-called Muenchian method. The idea is that the xsl:key
returns node sets that match each unique value of the key, in this case the
trunk number. You do a for-each, but only on those nodes that match the
***first*** one of the nodes returned by the unique keys. (Note - I wrapped
your example in a top-level element called "root").
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
version='1.0'>
<xsl:output method='html'/>
<xsl:key name='tr' match='tg' use='.'/>
<xsl:template match="/root">
<html>
<xsl:for-each
select="trunk-groups/tg[generate-id()=generate-id(key('tr',.)[1])]">
<xsl:value-of select="."/><br/>
</xsl:for-each>
</html>
</xsl:template>
</xsl:stylesheet>
Cheers,
Tom P
[Brucato, Greg]
> I am using the count() function to successfully count the number of nodes
> produced in XML data created using Java.
>
> However, I now have a situation where some of the values are duplicates.
Is
> there any way to count only unique values?
>
> Example: The count should be 6 not 8 since there are two duplicates in the
> second fragment.
>
> <xsl:value-of select="count(trunk-groups/tg)"/>
> ...
> <trunk-groups>
> <tg>T1-1-1-1</tg>
> <tg>T1-1-1-2</tg>
> <tg>T1-1-1-3</tg>
> <tg>T1-1-1-4</tg>
> </trunk-groups>
> ...
>
> <trunk-groups>
> <tg>T1-1-1-2</tg>
> <tg>T1-1-2-3</tg>
> <tg>T1-1-1-4</tg>
> <tg>T1-1-2-8</tg>
> </trunk-groups>
>
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list