Re: [xsl] Retrieving sequence of unique strings from another sequence

Subject: Re: [xsl] Retrieving sequence of unique strings from another sequence
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 05 Jan 2010 14:24:37 -0500
At 2010-01-05 13:41 -0500, Houghton,Andrew wrote:
I have a sequence of strings, e.g., ('abc', 'def', 'def', 'ghi'), and I want to create a new sequence that will have only the unique strings in it. The XSL 2.0 function distinct-values is not what I want because:

<xsl:variable name="list" as="xsd:string*" select="('abc', 'def', 'def', 'ghi')" />
<xsl:variable name="uniq" as="xsd:string*" select="distinct-values($list)" />


will result in a sequence of ('abc', 'def', 'ghi') which is not what I'm looking for. What I am looking for is the sequence ('abc', 'ghi'). Unix people will recognize that distinct-values() returns the result of the uniq command without any options. While what I'm looking for is the result of the uniq command with the -u option.

Can anyone suggest how I might use one or more XSL 2.0 functions to return only the unique strings.

I hope the solution below helps.


. . . . . . . Ken

T:\ftemp>xslt2 andy.xsl andy.xsl
abc ghi
T:\ftemp>type andy.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xsd="http://www.w3.org/2001/XMLSchema";
                version="2.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:variable name="list" as="xsd:string*"
                select="('abc', 'def', 'def', 'ghi')"/>
  <xsl:variable name="uniq" as="xsd:string*"
                select="for $each in $list return
                        if( count($list[.=$each])=1 ) then $each else ()"/>
  <xsl:value-of select="$uniq"/>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>


-- UBL and Code List training: Copenhagen, Denmark 2010-02-08/10 XSLT/XQuery/XPath training after http://XMLPrague.cz 2010-03-15/19 XSLT/XQuery/XPath training: San Carlos, California 2010-04-26/30 Vote for your XML training: http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread