XPath Functions

Estimated study time: 11 minutes.

XPath includes a library of built-in functions for working with strings, numbers, and node sets, letting you write more precise selections than paths alone allow.

String Functions

contains(title, 'Data')          <!-- true if title contains "Data" -->
starts-with(title, 'Data')       <!-- true if title starts with "Data" -->
substring(title, 1, 4)           <!-- first 4 characters of title -->
string-length(title)             <!-- length of the title text -->

Node-Counting Functions

count(//module)                  <!-- number of module elements -->
last()                           <!-- position of the last node in the set -->
position()                       <!-- position of the current node -->

Numeric Functions

sum(//price)                     <!-- total of all price values -->
round(4.7)                       <!-- 5 -->
floor(4.7)                       <!-- 4 -->
ceiling(4.2)                     <!-- 5 -->

Putting It Together

//course[contains(title, 'Data') and count(modules/module) > 3]

This selects course elements whose title contains "Data" and which have more than three modules.

💡 Tip: Combine string and counting functions inside a predicate to express complex selection logic in a single line, instead of writing multiple passes of filtering code.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with the Data Analytics Course at Uncodemy.

Explore XML Course →