XPath Predicates and Node-Set Evaluation

This article provides an overview of XPath predicates, explaining what they are, their syntax, and the exact mechanics behind how the XPath engine evaluates them to filter XML node-sets. You will learn about evaluation contexts, data type conversions, numeric indexing, and the behavior of chained predicates.

What Are XPath Predicates?

An XPath predicate is an expression enclosed in square brackets [...] that is appended to an XPath location step. Its primary role is to filter a sequence or node-set by testing each node against a specific condition. Only nodes that satisfy the condition are retained in the final result.

Syntax example:

/bookstore/book[price < 30]

In this expression, [price < 30] is the predicate applied to all <book> child elements of <bookstore>.

The Evaluation Process Against Node-Sets

When an XPath processor encounters a predicate, it does not evaluate the entire collection at once. Instead, it follows a structured iterative evaluation against the initial node-set:

1. Establishing the Context

For every node in the target node-set, the processor establishes an evaluation context consisting of three primary components:

2. Evaluating the Predicate Expression

The expression inside the brackets is executed once for each node using that node’s context. The expression produces a result, which is then converted to a boolean value based on the data type:

3. Filtering the Nodes

Chained Predicates

Multiple predicates can be applied sequentially to a single step:

//book[price < 30][1]

When predicates are chained: 1. The first predicate ([price < 30]) filters the original node-set to produce an intermediate node-set. 2. The second predicate ([1]) is evaluated against the intermediate node-set, meaning the context positions and context sizes are recalculated starting from 1.

In this example, the expression returns the first book whose price is under 30, rather than checking if the very first book in the document costs under 30.