RE: [xsl] GROUPING AND KEYS

Subject: RE: [xsl] GROUPING AND KEYS
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 28 Apr 2009 11:41:57 +0100
It's worth being clear that we're talking here about the Muenchian method of
grouping in XSLT 1.0. Grouping in XSLT 2.0 is much much simpler. 

> 1 - Is there a way to visualise in XSLT what this key looks like?
> 
> <xsl:key name="partNo" match="partNo" use="@num" />

You can think of it like this: "Each document that contains partNo elements
has an index, allowing a partNo element to be found quickly if the value of
its @num attribute is known". For example, key('partNo', 'P123') returns all
the partNo elements whose @num value is P123.
> 
> 
> In this statement <xsl:for-each 
> select="foo/shift/partNo[count(. | key('partNo', @num)[1]) = 1]">

In XSLT 1.0, there is no simple way of asking "$A is $B", that is, testing
whether two expressions refer to the same node. There are two common
workarounds to this:

generate-id($A) = generate-id($B)

which relies on the fact that if two nodes have the same generated ID then
they are the same node, and

count($A|$B) = 1

which relies on the fact that the union of two singleton sets is a singleton
only if both sets have the same member. In your example, this is the
expression you are seeing.

Specifically, it's selecting every partNo that is the first partNo with a
given @num value. That is "." (the partNo being tested) is the same node as
key('partNo', @num)[1], which is the first partNo with that @num value.
> 
> we are selecting all partNo nodes where count(. | key( 
> 'partNo', @num)[1]) = 1]
> 
> 2 - What does . mean in this expression? the partNo node?

Yes, in a predicate "." refers to the node being tested. 
> 
> 3 - What does | mean? 

It's a union operator, it forms the union of two sets.

Hope this helps.

Michael Kay
http://www.saxonica.com/

Current Thread