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

Subject: RE: [xsl] Retrieving sequence of unique strings from another sequence
From: "Houghton,Andrew" <houghtoa@xxxxxxxx>
Date: Tue, 5 Jan 2010 16:49:50 -0500
> From: Liam R E Quin [mailto:liam@xxxxxx]
> Sent: Tuesday, January 05, 2010 02:51 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] Retrieving sequence of unique strings from another
> sequence
>
> On Tue, 2010-01-05 at 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.
>
> [...]
>
> > Can anyone suggest how I might use one or more XSL 2.0 functions to
> > return only the unique strings.
>
> $sequence[count(index-of($sequence, .)) eq 1]
> maybe?

I think this is similar to what G. Ken Holman proposed:

  for $each in $list
  return if( count($list[.=$each])=1 ) then $each else ()

But both would probably preform slowly on large sequences.

Michael Kay suggested:

<xsl:for-each-group select="$list" group-by=".">
  <xsl:sequence select="current-group()[last()=1]"/>
</xsl:for-each-group>

which probably would preform better on large sequences however,
I needed to produce the result sequence as part of a complicated
XPath expression.  So I think the solution I'm leaning on is to
create an XSL 2.0 function:

<xsl:function name="my:unique-values" as="xsd:string*">
  <xsl:param name="values" as="xsd:string*" />
  <xsl:for-each-group select="$values" group-by=".">
    <xsl:sequence select="current-group()[last() = 1]" />
  </xsl:for-each-group>
</xsl:function>


Thank you all for the suggestions, Andy.

Current Thread