Skip to content

How to Use XPath Boolean Operators

How to combine XPath conditions using and, or, and not() to create precise filters for XML data extraction.

While comparison operators are essential for filtering nodes based on a single condition, the true power of XPath predicates [...] is unlocked when you combine multiple conditions. Boolean operators (and, or) and the not() function are the tools that allow you to build these complex, multi-part filters.

Core Concept: Combining Conditions in Predicates

Boolean operators are used inside the square brackets of a predicate [...] to link multiple comparison expressions together, creating a single, more powerful filter.

XPath Boolean Operators: and, or operators and the not() function

The and and or operators work just as they do in other programming languages:

Operator Description
and Returns true only if both conditions on either side are true.
or Returns true if at least one of the conditions on either side is true.

The not() is a function that reverses the boolean value of the expression passed to it. It is used to find nodes where a certain condition is false.

Practical Examples

All examples will refer to the following XML document:


<bookstore>
    <book category="cooking">
        <title>Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="children">
        <title>Harry Potter</title>
        <author>J. K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book category="web" cover="paperback">
        <title>Learning XML</title>
        <author>Tom Nolan</author>
        <year>2003</year>
        <price>39.95</price>
    </book>
</bookstore>

Using the and Operator

This is used when you need to select nodes that meet several criteria simultaneously.

Example: Select all books in the "cooking" category AND with a price of 30.00.

//book[@category='cooking' and price=30.00]

Output: this selects only the <book> for "Everyday Italian" because it is the only one that satisfies both conditions.


<book category="cooking">
    <title>Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
</book>

Using the or Operator

This is used when you need to select nodes that meet at least one of a set of criteria.

Example: Select all books that are in the "cooking" category OR the "children" category.

//book[@category='cooking' or @category='children']

Output: this selects both the "Everyday Italian" and "Harry Potter" <book> elements, as each one satisfies one of the conditions.


<book category="cooking">
    <title>Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
</book>
<book category="children">
<title>Harry Potter</title>
<author>J. K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

Using the not() Function

This is used to exclude nodes based on a condition.

Example: Select all books that do NOT have a "cover" attribute.

//book[not(@cover)]

Output: this selects the "Everyday Italian" and "Harry Potter" <book> elements, as the "Learning XML" book is the only one with a cover attribute.


<book category="cooking">
    <title>Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
</book>
<book category="children">
<title>Harry Potter</title>
<author>J. K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

Combining Multiple Operators

You can build highly specific queries by combining operators. Use parentheses () to group conditions and control the order of evaluation.

Example: Select all books that are in the "children" category OR (are published in 2003 AND have a price greater than 35.00).

//book[@category='children' or (year=2003 and price > 35.00)]

Output: this selects two <book> elements:

  • "Harry Potter" (because it matches the @category='children' condition).
  • "Learning XML" (because it matches the grouped year=2003 and price > 35.00 condition).

<book category="cooking">
    <title>Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
</book>
<book category="children">
<title>Harry Potter</title>
<author>J. K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

Conclusion

Boolean operators are essential for creating precise and powerful XPath queries. They allow you to move from simple, single-condition filters to complex, multi-faceted logic.

  • Use and when a node must satisfy all of your conditions.
  • Use or when a node must satisfy at least one of your conditions.
  • Use the not() function to select nodes where a condition is false.
  • Use parentheses () to group conditions and ensure your logic is evaluated in the correct order.
Navigation