XML technologies / XPath / XPath 2.0: language reference / Path expressions: location steps / Predicates (filter conditions)
(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, chapter 4.)
The node set remaining after an axis step can be further restricted by conditions called predicates.
The axis step must first be satisfied. Predicates are then evaluated successively for each resulting node, from left to right, with each expression filtering the result of its predecessor.
The predicate expression consists of an XPath expression yielding either a Boolean value (general predicate) or a decimal number identifying an item’s position in the input sequence (numeric predicate). The expression is enclosed in square brackets. Any number of predicate expressions may follow one another:
achsenschritt[predicate]*
Two predicates cannot simply be interchanged. The following expressions therefore generally do not produce the same result sequence:
achsenschritt[a][b]
is not equivalent to
achsenschritt[b][a]
An example is given below.
General predicates
A predicate filters a sequence by retaining or discarding items. A general predicate produces a Boolean value determining whether the tested node belongs in the result sequence:
$sequenz[bedingung]
The condition may be a comparison (for example, [@typ='warnung']) or an existential expression testing for a node’s presence (for example, [mein_element] tests for a child element mein_element).
Examples:
Full syntax:
Any axis step written in full syntax may have one or more general and/or numeric predicates filtering that step’s result sequence.
/descendant::bild[fn:position()=20]
selects the twentieth bild element in the document. This predicate uses a comparison expression evaluating the node’s position in the input sequence produced by the axis step.
/child::buchliste/child::buch[fn:position()=5]/child::kapitel[fn:position()=2]
selects the second kapitel element within the fifth buch element, a child of root element buchliste (the expression starts at the document node and follows the child axis to the document’s root element, an buchliste element).
child::abschnitt[attribute::typ="warnung"]
selects all abschnitt element nodes with attribute typ having value warnung.
child::*[self::kapitel or self::nachwort]
selects all child nodes kapitel and nachwort of the context node.
Abbreviated syntax:
Any axis step written in abbreviated syntax may have one or more general and/or numeric predicates filtering that step’s result sequence.
*[@typ]
selects an element node of any name that is a child of the context node and has attribute typ. The predicate thus contains an existential expression. It says nothing about the attribute’s value.
buch[@isbn]
An element buch that is a child of the context node must have attribute isbn. Its actual value does not matter.
buch[autor='James Joyce']
An element buch that is a child of the context node must have a child element autor whose string value is 'James Joyce'.
Numeric predicates
A numeric predicate checks whether an item’s position in the input sequence equals the numeric value in the predicate. The predicate’s value must always be an integer.
Example:
This numeric predicate selects the fifth item in the sequence and discards the rest:
$sequenz[5]
Any numeric predicate can also be written as a general predicate, so it is strictly only an abbreviation. Thus,
$sequenz[2]
is the same as
$sequenz[fn:position() = 2]
Successive predicates
Any axis step can be filtered by any number of predicates. Whether they are general or numeric is irrelevant, but their order matters:
abschnitt[@typ="warnung"][5]
selects the fifth child of the context node named abschnitt with attribute typ having value 'warnung' (that is, the fifth element among those with this attribute value).
abschnitt[5][@typ="warnung"]
selects the fifth child element abschnitt if it has attribute typ having value 'warnung'.
Caution — Predicate order matters
Comparing the preceding examples shows that predicate order matters. Filtering proceeds from left to right, so it makes a difference whether you write abschnitt[@typ= "warnung"][5] or abschnitt[5][@typ="warnung"].
In the first case, all abschnitt elements with the matching attribute value are selected first, then the fifth among them. In the second case, the fifth abschnitt element is tested for an attribute with the matching value. The result sequences may happen to coincide, but usually do not.
Copyright © Galileo Press, Bonn 2008
You may print the online version for private use.
Otherwise, this chapter from "XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn