Predicates

A predicate is an XPath expression that filters a node-set with respect to an axis and produces a new node-set. This filtering process involves sequentially evaluating the predicate against each node in the node set. Each time that the predicate is evaluated against a node:

  • The context node is the node currently being evaluated.

  • The context size is the number of nodes in the node-set being evaluated.

  • The context position is the position of the context node in the node-set.

This last context, that of the context node in the node-set, is relative to the direction that the axis specified in the location step navigates the document tree. Typically, an axis navigates the tree in either a forward or reverse direction:

  • A forward axis is an axis that contains the context node or nodes that are after the context node. The child::, descendant::, descendant-or-self::, following::, and following-sibling:: axes are forward axes. These forward axes number the nodes in the node-set in document order, starting with the first position as 1.

  • A reverse axis is an axis that contains the context node or nodes that are before the context node. The ancestor::, ancestor-or-self::, preceding::, and preceding-sibling:: axes are reverse axes. These reverse axes number the nodes in the node-set in reverse document order, starting with the first position as 1.

As to the remaining axes, the self:: and parent:: axes return a single node. Therefore, the designation of forward or reverse does not make sense for these two axes. There is no order defined for the attribute:: and namespaces:: axes; so they too are neither forward nor reverse axes.

Selecting the Nodes

A predicate expression evaluates to either a numeric or Boolean value.

If the predicate results in a number, XPath compares that number to the context position of the context node. If the number and the position are the same (that is, the context node is in the appropriate position in the tree), the predicate evaluates to true and the context node is included in the new node-set. Otherwise, the context node is excluded from the new node-set.

If the predicate does not result in a number, XPath uses the boolean function to convert the result to a Boolean value. For example, the predicate [genre='Computer'] results in a node-set. If the context node has a child <genre> element with the content Computer, this predicate evaluates to true and the context node is included in the new node-set. Otherwise, the context node is excluded from the new node-set.

NoteNote

A numeric predicate [x] is equivalent to the Boolean predicate [position()=x].

See Also

Reference

Location Steps
Axes
Node Tests