This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Selecting unique elements
- To: <xsl-list at mulberrytech dot com>
- Subject: Re: Selecting unique elements
- From: "Steve Muench" <Steve dot Muench at oracle dot com>
- Date: Thu, 9 Nov 2000 15:19:38 -0800
- References: <00BB1956AE40D411B5B60050DA27311F121B95@mail1.bluezone.net>
- Reply-To: xsl-list at mulberrytech dot com
| I'm building a transform for a saved ADO recordset and I need to select
| unique values. I thought that I could use
|
| //row[preceding-sibling::row/attribute::UserName !=
| attribute::UserName]/attribute::UserName
|
| It didn't work (well, it didn't return an error, it just didn't result in
| anything useful so either the current version of MSMXL doesn't have these
| features or I'm missed some important point.
|
| any suggestions?
You can use <xsl:key>'s to do this unique selection.
MSXSL 3.0 production release supports this, as does
Oracle XSLT, Saxon, and Xalan, among others perhaps.
Here's a stylesheet that produces a list of unique
user names for your XML input...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<xsl:output indent="yes"/>
<!-- Index rows based on their Username attribute -->
<xsl:key name="k" match="z:row" use="@UserName"/>
<xsl:template match="/">
<Users>
<!-- Select rows which *first* mention each unique username -->
<xsl:for-each select="/rs:data/z:row[generate-id(.)=generate-id(key('k',@UserName)[1])]">
<Name><xsl:value-of select="@UserName"/></Name>
</xsl:for-each>
</Users>
</xsl:template>
</xsl:stylesheet>
This produces:
<Users>
<Name>bar</Name>
<Name>foo</Name>
<Name>system</Name>
</Users>
______________________________________________________________
Steve Muench, Lead XML Evangelist & Consulting Product Manager
BC4J & XSQL Servlet Development Teams, Oracle Rep to XSL WG
Author "Building Oracle XML Applications", O'Reilly
http://www.oreilly.com/catalog/orxmlapp/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list