[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] removing duplicates from a sequence while retaining order


Subject: Re: [xsl] removing duplicates from a sequence while retaining order
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Tue, 09 Oct 2012 10:31:32 +0100

I expect you know that the Saxon implementation of distinct-values() does exactly what you want, but this doesn't help you much because it's not interoperable.

Perhaps

for $i in 1 to count($in)
return
  (if ($i = index-of($in, $in[$i])[1]) then $in[$i] else ())

It's O(n^2), of course, or worse if $in[$i] takes linear time, as it does in some implementations.

Michael Kay
Saxonica

On 09/10/2012 01:32, Birnbaum, David J wrote:
Dear XSLT list,

Is there an easy way in XPath (not using XSLT instructions) to select only the first occurrence of a specific value in a sequence of atomic values, so as to return the sequence in the original order, but with re-occurrences of each value after the first appearance of that value removed? For example, given an input sequence:

('Matthew','Mark','Luke','Matthew','John')

I want to produce

('Matthew','Mark','Luke','John')

I can't rely on using distinct-values() because that isn't guaranteed to keep specifically the *first* occurrence of a value.

In an XSLT context I can construct a temporary tree, poke each value into an element, all on the same level in the hierarchy, and then test for:

*[not(preceding-sibling::* = .)]

but I don't know how to write an XPath predicate that will filter the sequence of *atomic values* the way I want. Is this just a blind spot? Can anyone advise?

Thanks,

David
djbpitt@xxxxxxxxx


Current Thread
Keywords