Re: [xsl] Using xsl:key to list unique nested values

Subject: Re: [xsl] Using xsl:key to list unique nested values
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 08 Jan 2002 09:34:03 -0500
At 2002-01-08 12:46 +1100, Trem Stamp wrote:
I can get the listing of Unique customers

Customer 1
      Project 1
      Project 2
      Project 1

Customer 2
      Project 1
      Project 1

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).

Try doing only localized grouping with variables (solution below). You'll see that I am only grouping the projects on the collection of records for each customer.


I hope this helps.

................... Ken


t:\ftemp>type stamp.xml <PROJECTS> <PROROW> <id>1</id> <name>Customer 1</name> <project_name>Project 1</project_name> </PROROW> <PROROW> <id>2</id> <name>Customer 1</name> <project_name>Project 2</project_name> </PROROW> <PROROW> <id>3</id> <name>Customer 2</name> <project_name>Project 1</project_name> </PROROW> <PROROW> <id>3</id> <name>Customer 2</name> <project_name>Project 4</project_name> </PROROW> <PROROW> <id>3</id> <name>Customer 1</name> <project_name>Project 1</project_name> </PROROW> </PROJECTS>

t:\ftemp>type stamp.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:variable name="prorows" select="/*/PROROW"/>
  <xsl:for-each select="$prorows">
    <xsl:if test="generate-id(.)=
                  generate-id($prorows[name=current()/name])">
      <xsl:value-of select="name"/>:
<xsl:text/>
      <xsl:variable name="custs" select="$prorows[name=current()/name]"/>
      <xsl:for-each select="$custs">
        <xsl:if test="generate-id(.)=
                      generate-id($custs[project_name=
                                         current()/project_name])">
          <xsl:text>    </xsl:text>
          <xsl:value-of select="project_name"/>
          <xsl:text>
</xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>xt stamp.xml stamp.xsl stamp.txt

t:\ftemp>type stamp.txt
Customer 1:
    Project 1
    Project 2
Customer 2:
    Project 1
    Project 4

t:\ftemp>


-- Training Blitz: 3-days XSLT/XPath, 2-days XSLFO - Feb 18-22, 2002

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6                        Definitive XSLT & XPath
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1               Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
articles, training(instructor-live,Internet-live,web/CD,licensed)
Next public training:   2002-01-10,11,16,18,02-11,12,13,15,18,21,
-                                03-11,14,15,18,19,04-08,09,10,12


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



Current Thread