Re: [xsl] finding out distinct node/values

Subject: Re: [xsl] finding out distinct node/values
From: Hermann Stamm-Wilbrandt <STAMMW@xxxxxxxxxx>
Date: Wed, 10 Feb 2010 18:57:36 +0100
Hi Andrew,

you are right with O(n^2).

And your solution with xsl:key is fine.
Doing O(n) without xsl:key is possible but needs exslt:node-set()
and is not that nice as the xsl:key solution.

$ cat andrew.xsl
<xsl:stylesheet version="1.0"
  xmlns:exslt=""; >

  <xsl:key name="elems-by-value" match="*" use="."/>

  <xsl:template match="/">
    <xsl:variable name="name" select="'name'"/>

    <xsl:for-each select="//*[name() = $name][generate-id(.) = generate-id
(key('elems-by-value', .)[1])]">
      <xsl:copy-of select="."/>

$ xsltproc andrew.xsl anil.xml
<?xml version="1.0"?>
$ xsltproc anil2.xsl anil.xml
<?xml version="1.0"?>
$ cat anil2.xsl
<xsl:stylesheet version="1.0"
  xmlns:exslt=""; >

  <xsl:template match="/">
    <xsl:variable name="temp">
      <xsl:for-each select="/table/rows/row/name">
        <xsl:copy-of select="."/>

    <xsl:for-each select="exslt:node-set($temp)/*">
      <xsl:variable name="pos" select="position()"/>

      <xsl:if test=". != exslt:node-set($temp)/*[$pos - 1]">
        <xsl:copy-of select="."/>


Mit besten Gruessen / Best wishes,

Hermann Stamm-Wilbrandt
Developer, XML Compiler, L3
WebSphere DataPower SOA Appliances
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschaeftsfuehrung: Dirk Wittkopp
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294

             Andrew Welch
   >                                                  To
             02/10/2010 06:11                                           cc
                                       Re: [xsl] finding out distinct
             Please respond to         node/values


> <xsl:stylesheet version="1.0"
>  xmlns:xsl=""; >
>  <xsl:template match="/">.
>    <xsl:for-each select="/table/rows/row/name">
>      <xsl:if test="not(. = preceding::name)">
>        <xsl:copy-of select="."/>

While this is perfectly fine, it's worth being aware that its On^2....
in other words, as n (the number of elements that are selected)
increases by 1, it will have to check every other element in the
set.... which means it will perform badly for large values of n.

Andrew Welch

Current Thread