This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Using xsl:key to list unique nested values
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: "Trem Stamp" <trem at socialchange dot net dot au>
- Cc: XSL-List at lists dot mulberrytech dot com
- Date: Tue, 8 Jan 2002 07:50:59 +0000
- Subject: Re: [xsl] Using xsl:key to list unique nested values
- Organization: Jeni Tennison Consulting Ltd
- References: <018801c197e6$5bfb6980$74010a0a@socialchange.net.au>
- Reply-to: xsl-list at lists dot mulberrytech dot com
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(.) =
generate-id(key('rows-by-name-and-project_name',
concat(name, '+', project_name)))]" />
</xsl:template>
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()"
group-by="project_name">
<xsl:value-of select="project_name" />
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:template>
or:
<xsl:key name="rows" match="PROROW" use="name" />
<xsl:template match="PROJECTS">
<xsl:apply-templates select="distinct-values(PROROW/name)/.."
mode="other" />
</xsl:template>
<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>
<xsl:template match="PROROW" mode="again">
<xsl:value-of select="project_name" />
</xsl:template>
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list