Skip to content

How to Use XPath Predicates

How XPath predicates filter XML nodes using conditions like position, attributes, or child element values to precisely select data from hierarchical documents.

While XPath path expressions are excellent for navigating the hierarchy of an XML document, their true power comes from predicates. A predicate is a filter that allows you to select specific nodes from a larger set based on a condition. If an XPath expression is a question you ask an XML document, a predicate is the part that adds "where" or "which."

What is a XPath Predicate?

A predicate is an expression written inside square brackets []. It is appended to a path expression and acts as a filter on the set of nodes selected by that path. Only the nodes for which the condition inside the brackets is true will be returned.

Syntax: path/expression[condition]

Examples

Consider the following XML document:

<?xml version="1.0"  encoding="UTF-8"?>

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

Filtering by Position

This is the simplest type of filter. It allows you to select elements based on their order within a group of siblings. * Note:* XPath positions are 1-based, not 0-based.

Path Expression Result
/bookstore/book[1] Selects the first <book> element ("Everyday Italian").
/bookstore/book[last()] Selects the last <book> element ("Learning XML").
/bookstore/book[position()<3] Selects the first two <book> elements ("Everyday Italian" and "Harry Potter").

Filtering by Attribute

You can filter nodes based on the presence or value of their attributes. To refer to an attribute within a predicate, you must prefix its name with the @ symbol.

Path Expression Result
//title[@lang] Selects all <title> elements that have a lang attribute. (In this case, all three titles).
//book[@category='cooking'] Selects the <book> element where the category attribute has the exact value 'cooking'. ("Everyday Italian").

Filtering by the Value of a Child Element

This is one of the most powerful features of predicates. You can filter a set of parent nodes based on the content of their children.

Path Expression Result
/bookstore/book[price>35.00] Selects all <book> elements that have a child <price> element with a value greater than 35.00. ("Learning XML").
//book[author='J. K. Rowling'] Selects the <book> element whose child <author> has the exact value 'J. K. Rowling'. ("Harry Potter").
/bookstore/book[price>30.00]/title This demonstrates chaining. It first selects the book with a price over 30.00 ("Learning XML") and then, from that result, it selects the child <title> element.
Navigation