Skip to main content

How to Use XPath Node Functions and Operators

While XPath is excellent for navigating through an XML document's structure, its true power is revealed when you use its built-in functions and operators. These tools allow you to write advanced queries that filter nodes based on their * position, type, count, and other properties*, moving beyond simple name-based selection.

Let's explain the most important node-related operators and functions, showing you how to use them to create more precise and powerful XPath expressions.

note

All examples will refer to the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="style.css"?>
<bookstore>
<!-- Bestsellers List -->
<book category="cooking">
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
</book>
<book category="children">
<title>Harry Potter</title>
<author>J. K. Rowling</author>
</book>
<book category="web">
<title>Learning XML</title>
<author>Tom Nolan</author>
</book>
</bookstore>

XPath Path and Structure Operators

These operators are the fundamental building blocks of any XPath expression, used to define the path and structure of your query.

OperatorDescriptionPractical Example
/The Path Separator. Selects direct child nodes of the node on its left.bookstore/book selects the three <book> elements that are direct children of <bookstore>.
//The Recursive Descendant operator. Selects nodes anywhere in the document that match the selection, regardless of their position.//author selects all three <author> elements, no matter how deeply they are nested.
[...]The Predicate. A filter used to select specific nodes from a node-set based on a condition within the brackets.//book[@category='web'] selects only the <book> element that has a category attribute with the value 'web'.
|The Union Operator. Combines two node-sets, returning all nodes that exist in either set.//title | //author selects all <title> elements AND all <author> elements in the document.

XPath Node Functions

These are functions (called with ()) that provide information about nodes and are most often used inside predicates to perform advanced filtering.

Positional and Aggregate Functions

These functions operate on node-sets to determine their size or an element's position within them.

FunctionDescriptionPractical Example
position()Returns the 1-based index of the current node within its context.//book[position()=2] selects the second <book> element ("Harry Potter"). A common shorthand is //book[2].
last()Returns the index of the last node in the current context.//book[position()=last()] selects the last <book> element ("Learning XML"). A common shorthand is //book[last()].
count()Returns the total number of nodes in a given node-set.count(//book) returns the number 3. It can also be used in a predicate: //bookstore[count(book)=3].

Node Type Functions

These functions are used as a "node test" in a path step to select a specific type of node, rather than an element with a specific name.

FunctionDescriptionPractical Example
text()Selects the text content of the current node.//title/text() selects the three text nodes: "Everyday Italian", "Harry Potter", and "Learning XML".
comment()Selects any comment nodes.//comment() selects the <!-- Bestsellers List --> comment node.
processing-instruction()Selects any processing instruction nodes.//processing-instruction() selects the <?xml-stylesheet ... ?> node.
node()A wildcard node test that matches any type of node (element, text, comment, etc.).//book[1]/node() selects the <title> and <author> elements, plus the whitespace text nodes inside the first book.

Node Information Functions

This category of functions provides metadata about the nodes themselves.

FunctionDescriptionPractical Example
name()Returns a string containing the name of the current node.//*[name()='title'] selects all elements whose tag name is "title". This is equivalent to //title.

Conclusion

By combining the fundamental path operators with XPath's rich library of built-in node functions, you can write highly specific and powerful queries.

  • Use operators (/, //, |) to define the broad structure of your search.
  • Use functions (especially position(), last(), and count()) inside predicates ([...]) to filter those results based on their order, quantity, and other properties.
  • Use node-type functions (text(), comment()) in your path steps to select specific kinds of content.