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

Re: Using xsl:key to list unique nested values

Subject: Re: Using xsl:key to list unique nested values
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 8 Jan 2002 07:50:59 +0000
xsl key unique
Hi Trem,

> However I'm unsure as to how to only list the unique Projects within
> these listings. I've checked the archives and Dawsons (but couldn't
> find anything obvious).

To do grouping at two levels, you need the key for the second level to
take into account the first level of grouping as well. You need to
have two keys - the first as you have it, indexing the PROROW elements
by name, the second indexing the PROROW elements by name *and*
project_name. You can create a key that indexes an element by two
values by combining the values with the concat() function:

<xsl:key name="rows-by-name" match="PROROW" use="name"/>
<xsl:key name="rows-by-name-and-project_name" match="PROROW"
         use="concat(name, '+', project_name)"/>

Then amend your first-level grouping template (which you have in the
'other' mode) so that it applies templates to all those PROROW
elements of the same name (as retrieved with the first key), with a
unique value according to the second key:
<xsl:template match="PROROW" mode="other">
  <b><xsl:value-of select="name" /></b>
  <xsl:apply-templates mode="again"
    select="key('rows-by-name', name)
              [generate-id(.) =
                               concat(name, '+', project_name)))]" />

And there you have it.

For interest, with XSLT 2.0 you could use:

<xsl:template match="PROJECTS">
  <xsl:for-each-group select="PROROW" group-by="name">
    <b><xsl:value-of select="name" /></b>
    <xsl:for-each-group select="current-group()"
      <xsl:value-of select="project_name" />


<xsl:key name="rows" match="PROROW" use="name" />

<xsl:template match="PROJECTS">
  <xsl:apply-templates select="distinct-values(PROROW/name)/.."
                       mode="other" />

<xsl:template match="PROROW" mode="other">
  <b><xsl:value-of select="name" /></b>
  <xsl:apply-templates mode="again"
    select="distinct-values(key('rows', name)/project_name)/.." />

<xsl:template match="PROROW" mode="again">
  <xsl:value-of select="project_name" />



Jeni Tennison

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

Current Thread


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.
First Name
Last Name
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.