Re: select-distinct in for-each ?

Subject: Re: select-distinct in for-each ?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Mon, 4 Oct 1999 15:44:51 +0100 (BST)

> As you guess, I have duplicates that need to
> be removed -- I'm looking for a select-distinct.

This is a pretty common requirement and it may be interesting to
contemplate if xsl 1+n could offer some help for that.

At first glance it may seem that xsl:sort could have an option that said
to discard duplicates while sorting, but it probably isn't that simple.
if you do
<xsl:apply-templates select="x"/>
  <xsl:sort select="@foo"/>
</xsl:apply-templates>

then you get the x elements handed to you in the order controlled by
their foo attribute.

So, what to do if two <x foo="y">...</x> appear?

The `following::x' tests as used on this thread can fairly easily give you
either the first or the last such node, which means that only one
<x foo="y"> gets handled, but the important thing to note is that _it
makes a difference_ which one you use.  The sort key may be the same but
the actual x node may have different content (or different attributes)
so the result tree fragment constructed will depend on which node you
choose, unless the template is written to _only_ use the data that is
used for xsl:sort.

If their really is a requirement for finer level control over what to do
in the case of repeated sort keys, it may be that there is not much more
that can be done than the current method of programming the algorithm to
be used. On the other hand if it turns out that in 99% of the cases,
just taking the first node would be sufficient, then perhaps
<xsl:sort select="@foo" discard-duplicates="true()"/>
(or something)

might be of help.


David


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread