XPath Functions
XPath provides a rich library of built-in functions that work on strings, numbers, node-sets, and booleans. These functions let you extract, format, count, test, and manipulate data directly in your XPath expressions — without writing any application code.
Function Categories
- Node-set functions — work on groups of nodes
- String functions — process text values
- Numeric functions — perform calculations
- Boolean functions — test true or false conditions
Node-Set Functions
count()
Returns the number of nodes in a node-set.
count(/library/book) → total number of book elements count(//employee) → total employees anywhere count(/order/items/item) → number of items in the order
position()
Returns the position of the current node in the current node-set (starts at 1).
/products/product[position() = 1] → first product /products/product[position() < 4] → first three products
last()
Returns the total number of nodes in the current node-set. Used in predicates to target the last node.
/products/product[last()] → the last product /products/product[last() - 1] → the second-to-last product /products/product[position() = last()] → same as [last()]
name()
Returns the name of the current node as a string.
name() → the element name of the context node name(@*) → names of all attributes (one at a time in a loop)
local-name()
Returns the element name without any namespace prefix.
local-name() → "book" even if the element is ns:book
String Functions
string()
Converts a node to a string value.
string(price) → the text content of the price element string(42) → "42" string(true()) → "true"
concat()
Joins two or more strings together.
concat(firstName, ' ', lastName)
→ "Priya Menon" (with a space between)
concat('INR ', price)
→ "INR 1299"
string-length()
Returns the number of characters in a string.
string-length(name) → character count of name element
string-length('Hello') → 5
//product[string-length(name) > 10] → products with long names
substring()
Extracts part of a string. Syntax: substring(string, start, length)
substring('Bangalore', 1, 4) → "Bang"
substring('2024-03-15', 1, 4) → "2024" (extracts year from date)
substring('2024-03-15', 6, 2) → "03" (extracts month)
substring-before() and substring-after()
Returns the part of a string before or after the first occurrence of a delimiter.
substring-before('priya@email.com', '@') → "priya"
substring-after('priya@email.com', '@') → "email.com"
substring-before('2024-03-15', '-') → "2024"
substring-after('2024-03-15', '-') → "03-15"
contains()
Returns true if the first string contains the second string.
contains(name, 'Manager') → true if name has "Manager" in it
//employee[contains(role, 'Senior')] → employees with "Senior" in their role
contains('Hello World', 'World') → true
contains('Hello World', 'world') → false (case-sensitive)
starts-with()
Returns true if the first string starts with the second string.
starts-with(orderID, 'ORD-') → true for "ORD-2024-001" //product[starts-with(@sku, 'EL')] → products whose SKU starts with EL
normalize-space()
Strips leading and trailing whitespace, and collapses internal whitespace to single spaces.
normalize-space(' Hello World ') → "Hello World"
normalize-space(description) → cleans up description text
translate()
Replaces characters in a string. Each character in the second argument is replaced by the corresponding character in the third argument.
translate('Hello', 'aeiou', 'AEIOU') → "HEllO"
translate('hello world', ' ', '_') → "hello_world" (space to underscore)
translate(name, 'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
→ Converts name to UPPERCASE (XPath 1.0 method)
Numeric Functions
number()
Converts a value to a number.
number('42') → 42
number(price) → numeric value of the price element
number(true()) → 1
number(false()) → 0
number('abc') → NaN (not a number)
sum()
Returns the sum of numeric values in a node-set.
sum(/order/items/item/price)
→ total price of all items
sum(/report/row/quantity)
→ total quantity across all rows
floor(), ceiling(), round()
floor(3.7) → 3 (rounds down) ceiling(3.2) → 4 (rounds up) round(3.5) → 4 (standard rounding) round(3.4) → 3
Boolean Functions
boolean()
Converts a value to true or false. An empty string, 0, or empty node-set is false. Anything else is true.
boolean('') → false
boolean('hello') → true
boolean(0) → false
boolean(1) → true
boolean(/library/book) → true if at least one book exists
not()
Inverts a boolean value.
not(false()) → true not(contains(name, 'Manager')) → true if name does not contain "Manager" //employee[not(@status='inactive')] → all active employees
true() and false()
true() → always true (constant) false() → always false (constant)
Practical Combined Example
XML
<orders> <order id="O1"><customer> Divya Sharma </customer><total>1500</total></order> <order id="O2"><customer>Rohit Kumar</customer><total>320</total></order> <order id="O3"><customer>Ananya Singh</customer><total>8750</total></order> </orders>
XPath Expressions
count(/orders/order) → 3
sum(/orders/order/total) → 10570
/orders/order[total > 1000] → O1 and O3
normalize-space(/orders/order[1]/customer) → "Divya Sharma" (trimmed)
substring(/orders/order[3]/customer, 1, 6) → "Ananya"
concat('Order #', /orders/order[1]/@id) → "Order #O1"
Key Points to Remember
- count() counts nodes; sum() totals numeric values across a node-set.
- position() and last() work inside loops to identify node position.
- contains() and starts-with() test string membership — both are case-sensitive.
- concat() joins strings; substring() extracts parts; normalize-space() cleans whitespace.
- floor(), ceiling(), and round() handle decimal numbers.
- not() inverts a boolean; boolean() tests if a value is truthy.
- XPath 1.0 has no uppercase() function — use translate() instead.
