Re: Selecting unique elements

Subject: Re: Selecting unique elements
From: "Steve Muench" <Steve.Muench@xxxxxxxxxx>
Date: Thu, 9 Nov 2000 15:19:38 -0800
| 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


Current Thread