Re: [xsl] What does this XPath expression mean ?

Subject: Re: [xsl] What does this XPath expression mean ?
From: Kamal <kbhatt@xxxxxxxxx>
Date: Sat, 22 Mar 2008 23:43:21 +1100
I was in the middle of answering this, and I didn't want my good work to go to waste, so I thought I would answer again.

Just looking at this:

[not(@label = preceding::*/@label)]


It is comparing the current label is exactly the same as the preceding labels. For example,


<blah label="A">1</blah>
<blah label="A">2</blah>
<blah label="B">3</blah>
<blah label="B">4</blah>
<blah label="C">5</blah>

This will match on 2, 4, 5. As you can gather, that XPath is pretty much useless without your dataset being sorted. This is an inefficient way of grouping values. Your other post about the generate-id is a more efficient way of doing this. Personally speaking, if you are not worried too much about efficiency, I recommend sticking with this method. It is easier to understand and doesn't rely on keys.

Justin Johansson wrote:
The for-each expression is to iterate over a selected nodeset, that being
defined by the select attribute (yes, you knew that)

The selected nodeset is all of the child nodes of the 'testResults'
element, X, filtered by a predicate, P.

The unfiltered nodeset, X, is specified by the partial expression,
'/testResults/*'.

The predicate is 'not(@label = preceding::*/@label)'.

So the final result nodeset is all the nodes selected by X excluding the
nodes not statisifed by the predicate, P.

My guess is that you are having trouble interpreting the meaning of the
predicate.  If this the case, you may find the answer you require as follows:

The predicate (or filter expression) is the logical complement of

@label = preceding::*/@label

which factors out the not() wrapper.

So that leaves you with understanding that the nodes (rather, elements)
*not* to be included in the result
are all the elements with an attribute named 'label' are to be equal to any
preceding element with the same label.

Now that is more-or-less of the matter but there are some finer points of
XPath semantics that others on this fine list will no doubt amplify.

Hope this helps, though my answer is not complete in exact detail.

Cheers,

Justin Johansson




Could someone be kind to help me breaking down the XPath to explain what
the expression means ?
<xsl:for-each select="/testResults/*[not(@label = preceding::*/@label)]">

Current Thread