Re: set:distinct - exsl:node-set question

Uche Ogbuji <[email protected]>
Newsgroups gmane.text.xml.xslt.extensions
Organization Fourthought, Inc.
Message-ID <1086630427.17093.5270.camel@borgia>
On Tue, 2004-06-01 at 06:35, Pettit, Chad E (Xontech) wrote:
> How do you use set:distinct with a variable?
> 
> ex:
> 
> XML:
> <objects>
> 	<object>1</object>
> 	<object>2</object>
> 	<object>1</object>
> </objects>
> 
> XSL:
> <xsl:variable name="objectList">
> 	<xsl:for-each select="object">
> 		<test><xsl:value-of select="."/></test>
> 	</xsl:for-each>
> </xsl:variable>
> 
> 
> <xsl:for-each select="set:distinct(exsl:node-set($objectList))">
> 	<xsl:copy-of select="."/>
> </xsl:for-each>
> 
> This prints out the entire list.  I would expect is to eliminate one of
> the <object>1</object> elements.  The set:distinct works when I do
> set:distinct(//object).  Unfortunately, the real use of this function is
> much more complicated than this simple example and an intermediate
> node-set is necessary.
> 
> Any help would be greatly appreciated.

This works:

<xsl:transform
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common"
  xmlns:set="http://exslt.org/sets"
  exclude-result-prefixes="exsl set"
>
                                                                                
  <xsl:template match="objects">
    <xsl:variable name="objectList">
      <xsl:for-each select="object">
        <test><xsl:value-of select="."/></test>
      </xsl:for-each>
    </xsl:variable>
                                                                                    <xsl:for-each select="set:distinct(exsl:node-set($objectList)/test)">
      <xsl:copy-of select="."/>
    </xsl:for-each>
  </xsl:template>
                                                                              </xsl:transform>
 
n.b. the trailing "/test" in my set:distinct call.  Remember that the
result of exsl:node-set is usually a root node, which is what you're
passing to set:distinct.  You actually want to pass the children of that
root node.  To see what I mean, try these:

    <xsl:value-of select="count(exsl:node-set($objectList))"/>, 
    <xsl:value-of select="count(exsl:node-set($objectList)/test)"/>, 
    <xsl:value-of
select="count(set:distinct(exsl:node-set($objectList)))"/>, 
    <xsl:value-of
select="count(set:distinct(exsl:node-set($objectList)/test))"/>

You'll get 1, 3, 1, 2.


-- 
Uche Ogbuji                                    Fourthought, Inc.
http://uche.ogbuji.net    http://4Suite.org    http://fourthought.com
A survey of XML standards - http://www-106.ibm.com/developerworks/xml/library/x-stand4/
When to use elements versus attributes - http://www-106.ibm.com/developerworks/xml/library/x-eleatt.html
Introducing PyRXP - http://www.xml.com/pub/a/2004/02/11/py-xml.html
XML in the financial services industry - http://www-106.ibm.com/developerworks/xml/library/x-think22.html
Python Web services developer: The real world, Part 2 - http://www-106.ibm.com/developerworks/webservices/library/ws-pyth16/
Keep your XML clean - http://www.adtmag.com/article.asp?id=9012
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.