Velocity Reviews > XML > Re: Can someone explain this snippet? Muenchian sorting.

# Re: Can someone explain this snippet? Muenchian sorting.

Colin Mackenzie
Guest
Posts: n/a

 07-03-2003
see below

"Jim Bancroft" <(E-Mail Removed)> wrote in message
news:Q0YMa.35\$(E-Mail Removed)...
> I've been reading up on the "Muenchian" sorting method on
> www.jenitennison.com, and saw this use of the count() function:
>
> <xsl:for-each select="contact[count(. | key('contacts-by-surname',
> surname)[1]) = 1]">
>
> What's happening is the code is trying to select the first node in a
> set, given a particular key. At least that's what I think.
>
> Anyway, the "count(. | key('contacts-by-surname', surname)[1]) = 1"

bit
> is throwing me for a loop, I can't figure out what's going on there. Are
> you trying to OR the current context node with the first value of the node
> set returned from the key() function? What happens when you do that?
>

the current context node . is being OR'd (a "set" operation) with the first
node returned from the key (as you say) to contain a node set with a
combination of both nodes
IF the current node IS the same node as that returned from the key then the
combined node set will only contain ONE node, therfore count(the combined
nodeset) would = 1
Therefore the count = 1 is True for that node, the [] predicate is true and
the node is processed by the logic within for-each (and so you get the first
name in the set presumably)

> Another way of doing the same thing seems to be as follows, (and

thanks
> to Marrow for helping me with a similar problem yesterday):
>
> "contact[generate-id() = generate-id(key('contacts-by-surname',
> surname)[1])]"
>
> I understand this a little more, it looks like you want the node whose
> id (conveniently created via "generate-id()") matches the first one

returned
> by the key. Please correct me if I misunderstood that or any other

portion
> of the code I posted. Thanks!

yes you are correct, but remeber the predicate [] portion is resolving to
true or false and can return true for multiple nodes (the first occurence of
a surname in a list)

>
> -Jim
>
>
>
>
>
>
>
>