Distinct values in a exslt nodeset

363 views Asked by At

I have a program that receives a nodeset from a function in exslt. It contains duplicate nodes (Tom Waits appears twice) :

<xsl:template name="giveMeHeroes">
    <person>
        <lastName>Waits</lastName>
        <firstName>Tom</firstName>
    </person>
    <person>
        <lastName>Everett</lastName>
        <firstName>Mark</firstName>
    </person>
    <person>
        <lastName>Hickey</lastName>
        <firstName>Rich</firstName>
    </person>
    <person>
        <lastName>Waits</lastName>
        <firstName>Tom</firstName>
    </person>
</xsl:template>

<xsl:template match="/">

    <xsl:variable name="someHeroes">
        <xsl:call-template name="giveMeHeroes"></xsl:call-template>
    </xsl:variable>


    <xsl:apply-templates select="ext:node-set($someHeroes)/person"/>

</xsl:template>


<xsl:template match="person">
    <xsl:value-of select="concat('Long live',firstName,' ',lastName,'!!!')"/>
    <br/>
</xsl:template>

This example yields (parsed in a browser ) :

Long live Tom Waits!!!
Long live Mark Everett!!!
Long live Rich Hickey!!!
Long live Tom Waits!!! 

I know I should be able to filter the results with set:distinct(nodeset) , something in the lines of <xsl:apply-templates select="set:distinct(ext:node-set($someHeroes)/person)"/> maybe, but somehow I cannot find the way to do that. Any help would be appreciated.

1

There are 1 answers

1
Martin Honnen On BEST ANSWER

Your code should do and works for me with Saxon 6.5.5 in http://xsltransform.net/gWmuiJX, it does

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
  xmlns:exsl="http://exslt.org/common"
  xmlns:set="http://exslt.org/sets"
  exclude-result-prefixes="exsl set">

    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

<xsl:template name="giveMeHeroes">
    <person>
        <lastName>Waits</lastName>
        <firstName>Tom</firstName>
    </person>
    <person>
        <lastName>Everett</lastName>
        <firstName>Mark</firstName>
    </person>
    <person>
        <lastName>Hickey</lastName>
        <firstName>Rich</firstName>
    </person>
    <person>
        <lastName>Waits</lastName>
        <firstName>Tom</firstName>
    </person>
</xsl:template>

<xsl:template match="/">

    <xsl:variable name="someHeroes">
        <xsl:call-template name="giveMeHeroes"></xsl:call-template>
    </xsl:variable>


    <xsl:apply-templates select="set:distinct(exsl:node-set($someHeroes)/person)"/>

</xsl:template>


<xsl:template match="person">
    <xsl:value-of select="concat('Long live',firstName,' ',lastName,'!!!')"/>
    <br/>
</xsl:template>
</xsl:transform>

and outputs Long liveTom Waits!!!<br>Long liveMark Everett!!!<br>Long liveRich Hickey!!!<br>.