Re: [xsl] union/intersection on attribute values

Subject: Re: [xsl] union/intersection on attribute values
From: David Carlisle <davidc@xxxxxxxxx>
Date: Sat, 21 Apr 2001 23:06:03 +0100
> DC, can you elaborate your approach using count()?
'tisn't really mine, but anyway
in the beginning, the general method of gettin rid of duplicates (or
equivalently getting the first item in each group of related items.
was to go something like


ie  select all foo's that don't have the same value as an earlier one.

This works but has quadratic behaviour in the number of nodes being

Steve Meunch had an insight into using xsl:key to improve things.
This is populatised by Jeni, who has a good description of it
at her site. It's the method I used in my "dynamic" example.
First you specify a key, then  a node is the first item in a group 9ie
the one you want if you are discarding duplicates) if it is the first
node in the node set returned by the key.

The only trick part is that XSL doesn't have a node identity test.
Given a node $x and a node set $Y how do you tell if $x is in $Y.

two basic methods, one uses generate-id to give strings which you can
compare to give node identity test, the other uses set theory.

count($x | $Y) is the number of elements in the set Y union the singlton
set $x. This will be equal to count($Y) if $x is already in $Y and equal
to count($Y)+1 otherwise.



selects all element child nodes of record for whic
count(.|key('x',name())[1])=1 (rather than 2) ie it selects those
nodes that are in the set  key('x',name())[1] ie are the first node
of each group. IE subsequent duplicates are not selected.


This message has been checked for all known viruses by Star Internet delivered
through the MessageLabs Virus Control Centre. For further information visit

 XSL-List info and archive:

Current Thread