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:
- Node-sets: Evaluates to
trueif the node-set contains at least one node;falseif it is empty. - Strings: Evaluates to
trueif the string length is greater than zero;falseif it is an empty string (""). - Numbers: Evaluates to
trueif the number is non-zero and notNaN;falseif it is0,-0, orNaN. - Booleans: Retains its literal
trueorfalsevalue.
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):
- Parentheses
()and Function Calls (e.g.,not()): Expressions inside parentheses or functions are evaluated first. andOperator: Conjunction takes precedence over disjunction.orOperator: 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:
- For
A and B: IfAevaluates tofalse, the processor immediately returnsfalsewithout evaluatingB. - For
A or B: IfAevaluates totrue, the processor immediately returnstruewithout evaluatingB.
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.