Evaluating Boolean Operators in XPath Predicates

This article explains how boolean operators—specifically and, or, and the not() function—are evaluated within XML XPath predicates. It covers the rules of operator precedence, short-circuit evaluation behavior, and how non-boolean data types are implicitly converted into boolean values (Effective Boolean Value) to determine which XML nodes are selected.


Predicate Context and Truth Values

In XPath, a predicate is an expression enclosed in square brackets ([...]) appended to a node-set expression. It filters nodes by testing whether the expression inside the brackets resolves to true or false for each context node.

When expressions inside a predicate do not natively produce a boolean, XPath converts them into an Effective Boolean Value (EBV) according to standard coercion rules:


The Operators: and, or, and not()

1. The and Operator

The and operator performs logical conjunction. It evaluates to true if and only if both its left and right operands evaluate to true.

//book[@price < 30 and @in-stock='true']

Selection rule: Selects <book> elements where the @price attribute is strictly less than 30 and the @in-stock attribute equals 'true'.

2. The or Operator

The or operator performs logical disjunction. It evaluates to true if at least one of its operands evaluates to true.

//employee[@role='Admin' or @role='Manager']

Selection rule: Selects <employee> elements where the @role is either 'Admin', 'Manager', or both.

3. The not() Function

Unlike and and or, which are infix operators, negation in XPath is handled via the core library function not(). It accepts an argument, converts it to its Effective Boolean Value, and returns the inverse.

//product[not(@discontinued)]

Selection rule: Selects <product> elements that do not possess a @discontinued attribute (or where the attribute evaluates to false).


Operator Precedence

When multiple boolean operators are combined in a single predicate, XPath applies the following order of precedence (from highest to lowest):

  1. Parentheses () and Function Calls (e.g., not()): Expressions inside parentheses or functions are evaluated first.
  2. and Operator: Conjunction takes precedence over disjunction.
  3. or Operator: Disjunction is evaluated last.

Example of Precedence:

//item[@active='true' or @featured='true' and @priority='high']

Because and has higher precedence than or, the expression is implicitly evaluated as:

//item[@active='true' or (@featured='true' and @priority='high')]

To force or to evaluate before and, explicit parentheses must be used:

//item[(@active='true' or @featured='true') and @priority='high']

Short-Circuit Evaluation

Standard XPath implementations use short-circuit evaluation for boolean expressions to optimize performance:

This allows safe condition chaining, such as checking for the existence of an element or attribute before evaluating its specific value or running a computationally expensive function on it.