XPath (XML Path Language) is a query language used to select nodes from an XML document. One of the key features of XPath is predicates, which allow you to filter nodes based on specific conditions.In this chapter, we will explore what XPath predicates are, how they work, and provide examples from basic to advanced levels. By the end, you will be able to use XPath predicates effectively in XML documents.
A predicate in XPath is an expression that appears inside square brackets ([]
) and is used to filter nodes from a set of nodes. It allows you to narrow down a selection by adding conditions or constraints.
/path/to/element[condition]
Let’s consider a simple XML file:
Harry Potter
J.K. Rowling
The Hobbit
J.R.R. Tolkien
The Alchemist
Paulo Coelho
If you want to select the second <book>
element in the <library>
, you can use a predicate.
/library/book[2]
/library/book
selects all <book>
elements.[2]
specifies the second <book>
.
The Hobbit
J.R.R. Tolkien
If an element contains an attribute, you can filter nodes based on that attribute’s value. Consider the following XML structure:
The Hobbit
J.R.R. Tolkien
Becoming
Michelle Obama
Example 1: Select books in the “fiction” category
/library/book[@category='fiction']
/library/book
selects all <book>
elements.[@category='fiction']
filters books where the category
attribute is fiction
.
The Hobbit
J.R.R. Tolkien
You can combine predicates with XPath functions like contains()
, starts-with()
, and last()
to build more complex queries.
contains()
functionIf you want to find all books whose title contains the word “The”:
/library/book[contains(title, 'The')]
contains(title, 'The')
checks if the <title>
element contains the string “The”.
The Hobbit
J.R.R. Tolkien
Becoming
Michelle Obama
last()
functionTo select the last book in the library:
/library/book[last()]
last()
returns the last element in the node set.
Becoming
Michelle Obama
You can use multiple conditions in a single predicate with logical operators like and
and or
.
/library/book[@category='fiction' and contains(title, 'Hobbit')]
[@category='fiction' and contains(title, 'Hobbit')]
ensures that the book is in the fiction
category and its title contains the word “Hobbit”.
The Hobbit
J.R.R. Tolkien
Select books with titles longer than 10 characters:
/library/book[string-length(title) > 10]
string-length(title)
gets the length of the title string.
The Hobbit
J.R.R. Tolkien
Becoming
Michelle Obama
[]
) and allow you to filter nodes.contains()
, last()
, string-length()
can be used inside predicates for advanced queries.and
, or
) and numeric comparisons to refine your results.XPath predicates are powerful tools to query XML documents efficiently. By mastering predicates, you can extract specific nodes, filter data based on attributes or content, and even apply functions to tailor your search. This chapter has walked you through basic to advanced use cases of XPath predicates. With the examples provided, you should now be able to use predicates effectively in your XML projects. Happy coding !❤️