[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Anyone can explain me this syntax?


Subject: Re: [xsl] Anyone can explain me this syntax?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 13 Jan 2004 12:27:37 -0500

Jaime,

To add to others' responses ... the point of the expression is to select a single node that can serve as a representative or "flagbearer" node for a group of nodes. If we know exactly which group we want, this is not hard -- for example, in your case if we want the group of nodes matching the key 'relacion_x_cobertura' with REL_ID value of 'x', we'd say key('relacion_x_cobertura', 'x')[1] -- that is, the first node (the [1] predicate) in document order of the set returned by the key function with value 'x' as the key.

But what if we don't want a particular unique flagbearer, but one flagbearer each for *all* the key values in our document? The only way to do this in XSLT 1.0 is to collect all the candidates and test each one to see if it's the designated flagbearer for its set (throwing away the ones that aren't). This means we have to compare each node in turn to key('relacion_x_cobertura', 'x')[1], and see if it's the same node. (Fortunately this test can be performed in the predicate of a path expression that would otherwise select all the candidates.)

Since XSLT 1.0 does not have a real node-identity test, we can't do this by simply comparing them. Accordingly we have a workaround test, which takes the form of

count($node | $nodeset[1]) = 1

this will be the case if $node and $nodeset[1] are the same, but not if they are two different nodes. (This is why your test worked when you said [2] instead of [1], since [2] picks a single node just as [1] does. But yes, you were lucky: if any of your sets didn't have a second member, returning an empty node set from $nodeset[2], you'd be sunk.)

Another way to perform this same test is to say

generate-id($node)=generate-id($nodeset[1])

which is why we also see the Muenchian technique used in the form

ROW[generate-id(.) = generate-id(key('relacion_x_cobertura', REL_ID)[1])]

which can be abbreviated as
ROW[generate-id() = generate-id(key('relacion_x_cobertura', REL_ID))]

since the generated ID (a string) of a set of nodes will be the generated ID of the first node in the set.

The essence of the Muenchian grouping technique could be summarized as "collect the nodes to be grouped and de-duplicate by the grouping criterion to select a set of unique flagbearers; iterate over that set and with each iteration take the flagbearer and all the nodes in its group".

I hope this helps--
Wendell

At 10:58 AM 1/13/2004, you wrote:
Hi all...

>From Muenchian method of grouping, I always use something like this:

ROW[count(. | key('relacion_x_cobertura', REL_ID)[1]) = 1]

That always works, but I want to understand he reason of that syntax, so that if I don't have the possibility of doing copy & paste from other code, I could be able to write that expression by myself.

I know that instruction only returns the different elements contained in ROW. That difference is determined by REL_ID value.

But the actual questions are:

- Why the . (dot) is used? why if I omit it, it doesn't work (it returns all elements)?
- What's the meaning of the | (pipe)?
- What's the meaning of [1]? I have always used things like [FIELD_NAME=some_value]. I understand that perfectly, but what about placing only that number in the brackets? I tried by using [2] and it worked too... or, perhaps I was lucky?


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


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




Current Thread
Keywords