Re: [xsl] xsl:key grouping problem

Subject: Re: [xsl] xsl:key grouping problem
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 17 Nov 2004 14:35:26 GMT
> Using xsl keys seems like a tough concept to
> understand(atleast for me). Are there other novices

keys themselves are simple enough, but using them (as here) for grouping
isn't at all obvious and is normally called (here at least) Muenchian
grouping after Steve Muench who first thought of this.
Jeni's site is the usual reference for a tutorial.

http://www.jenitennison.com/xslt/grouping/index.html

>  How does generate-id on country be equal to
>  generate-id on the key? That's where I am confused.
>  country[generate-id() = generate-id(key('groups',
>  concat(country_group, '2004', '2'))) ??


The trick here is that if you are on a country then
concat(country_group, '2004', '2')
is the key string you want to use and so

key('groups', concat(country_group, '2004', '2'))

will return all the nodes that we want to consider to be grouped with
this country.

Now the test is trying to say "is this node the first node in this
group" (if so start processing the group, if not do nothing as this node
was already handled when the rest of the group was handled, on the first
node)

so this node is .

and the first node in the current group is

key('groups', concat(country_group, '2004', '2'))[1]

so you want to know if

. is key('groups', concat(country_group, '2004', '2'))[1]

now in XPath 2 (draft) there is an "is" operator that tests node
identity and the above line is actually valid Xpath2 but in Xpath 1
there is no operator. You can't "is". You can't use use = as


. = key('groups', concat(country_group, '2004', '2'))[1]

would test if the string value of the current node was equal to the
string value of the first node in the group, which isn't what we want.

however generate-id returns a unique string for each node so you can say

generate-id(.) = generate-id(key('groups', concat(country_group, '2004', '2'))[1])

and that will be true just if the current node is the first node in the
group.

This idiom isn't at all obvious but it is a FAQ and when you've seen it
most days on this list for 5 years you tend to take some syntactic
shortcuts:

generate-id(.)

can be written

generate-id() because . is implied if you supply no argument.

generate-id(key('groups', concat(country_group, '2004', '2'))[1])

can be written

generate-id(key('groups', concat(country_group, '2004', '2')))

because as for most string generating functions in XSLT 1, if you supply
a node set as an argument it will take the first node in teh set in
document order and use that and silently ignore any other nodes, in
other words [1] is implictly applied.

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread