Reputation: 3944
Structure of a document:
<program>
<projectionDay>
<projection/>
<projection/>
</projectionDay>
<projectionDay>
<projection/>
<projection/>
</projectionDay>
</program>
I want to select the first and last projection ( across the whole document).
This returns it:
/descendant::projection[position() = 1 or position() = last()]
This returns first and last within a projectionDay
//projection[position() = 1 or position() = last()]
Why is that so?
Upvotes: 1
Views: 62
Reputation: 38672
Your first query using descendant
fetches all <projection/>
elements, then filters this result set for the first and last element:
/descendant::projection[position() = 1 or position() = last()]
//
is an abbreviation for /descendant-or-self::*/
. So your second query actually means
/descendant-or-self::*/projection[position() = 1 or position() = last()]
which looks into all elements (here: each <projectionDay/>
, and returns the first and last <projection/>
element inside this element.
To return the first and last element over all <projeectionDay/>
s, put everything before the predicate into parentheses:
(/descendant-or-self::*/projection)[position() = 1 or position() = last()]
or abbreviated:
(//projection)[position() = 1 or position() = last()]
Upvotes: 5