Re: [xsl] Distinct list problem

Subject: Re: [xsl] Distinct list problem
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 18 Jan 2002 09:10:23 -0500
At 2002-01-18 14:53 +0100, Henk Meulekamp wrote:
I'm trying to get a list of id's where each id is represented once out of
the xml below. Is it possible?

Yes, and there are a few ways you can do it.


The ouput I want is:

20
21
22

This is a classic "grouping" problem. Two of the ways you can do it are with key tables or variables, both illustrated below using *only* XSLT 1.0 functionality and no vendor extensions.


In each case one is relying on the generate-id() function to uniquely identify nodes from a set. One can use a key table for the set, or if you are using XT one can use a variable for the set. When doing grouping of subsets of the tree, using a variable for the set is often more convenient than using the key table.

I hope this helps.

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


T:\ftemp>type henk.xml <CONTRACT IDCONTRACT="2" CCONTRACTNUMBER="1.1" CPRODUCT="JORISTEST" DECQUANTITY="1000.000" CUNIT="MT"> <DELIVERY IDDELIVERY="1" NUMLOADINGASSIGNMENT="1" IDTESTREPORT="2"> <ATTRIBUTEQUALITY IDTESTTYPE="20" /> <ATTRIBUTEQUALITY IDTESTTYPE="21" /> </DELIVERY> <DELIVERY IDDELIVERY="2" NUMLOADINGASSIGNMENT="1234" IDTESTREPORT="1"> <ATTRIBUTEQUALITY IDTESTTYPE="21" /> <ATTRIBUTEQUALITY IDTESTTYPE="20" /> <ATTRIBUTEQUALITY IDTESTTYPE="21" /> <ATTRIBUTEQUALITY IDTESTTYPE="21" /> <ATTRIBUTEQUALITY IDTESTTYPE="20" /> </DELIVERY> <DELIVERY IDDELIVERY="4" NUMLOADINGASSIGNMENT="4" IDTESTREPORT="20"> <ATTRIBUTEQUALITY IDTESTTYPE="22" /> <ATTRIBUTEQUALITY IDTESTTYPE="21" /> <ATTRIBUTEQUALITY IDTESTTYPE="20" /> </DELIVERY> <DELIVERY IDDELIVERY="6" NUMLOADINGASSIGNMENT="6" IDTESTREPORT="23"> <ATTRIBUTEQUALITY IDTESTTYPE="21" /> <ATTRIBUTEQUALITY IDTESTTYPE="20" /> </DELIVERY> </CONTRACT>

T:\ftemp>type henk.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:key name="ids" match="ATTRIBUTEQUALITY" use="@IDTESTTYPE"/>

<xsl:template match="/">

  <xsl:text>using keys:
</xsl:text>
  <xsl:for-each select="//ATTRIBUTEQUALITY
                        [generate-id(.)=
                         generate-id(key('ids',@IDTESTTYPE))]">
    <xsl:value-of select="@IDTESTTYPE"/>
    <xsl:text>
</xsl:text>
  </xsl:for-each>

<xsl:text>

using variables:
</xsl:text>
  <xsl:variable name="ids" select="//ATTRIBUTEQUALITY"/>
  <xsl:for-each select="$ids">
    <xsl:if test="generate-id(.)=
                  generate-id($ids[@IDTESTTYPE=current()/@IDTESTTYPE])">

      <xsl:value-of select="@IDTESTTYPE"/>
      <xsl:text>
</xsl:text>
    </xsl:if>
  </xsl:for-each>

</xsl:template>

</xsl:stylesheet>

T:\ftemp>saxon henk.xml henk.xsl
using keys:
20
21
22


using variables: 20 21 22


-- Training Blitz: 3-days XSLT/XPath, 2-days XSLFO - Feb 18-22, 2002 - (Early-bird date for discounts is today!)

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-18,02-11,12,13,15,18,21,
-              03-04,05,06,08,11,04-08,09,10,12,05-14,15,06-04,07


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



Current Thread