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:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:exslt="http://exslt.org/common"; >

  <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="."/>
    </xsl:for-each>

  </xsl:template>
</xsl:stylesheet>
$ xsltproc andrew.xsl anil.xml
<?xml version="1.0"?>
<name>A</name><name>B</name><name>C</name>
$ xsltproc anil2.xsl anil.xml
<?xml version="1.0"?>
<name>A</name><name>B</name><name>C</name>
$ cat anil2.xsl
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:exslt="http://exslt.org/common"; >

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

    <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="."/>
      </xsl:if>
    </xsl:for-each>

  </xsl:template>
</xsl:stylesheet>
$


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
             <andrew.j.welch@g
             mail.com>                                                  To
                                       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
             02/10/2010 06:11                                           cc
             PM
                                                                   Subject
                                       Re: [xsl] finding out distinct
             Please respond to         node/values
             xsl-list@xxxxxxxx
              lberrytech.com








Hi,

> <xsl:stylesheet version="1.0"
>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; >
>  <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
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread