XPath Reference

String Functions

String functions let you inspect, slice, and transform text content directly inside an XPath expression — no need to pull values into a host language first. The XPath 1.0 set is small but covers the essentials: containment checks, prefix and suffix tests, substring extraction, length, whitespace normalization, and character translation.

string()

Convert to string

Converts a value to its string representation. When called on a node-set, it returns the string value of the first node. When called on a number, it produces the decimal representation. It's useful for explicit type conversion and for getting the full text content of an element including all descendant text.

concat(a, b, …)

Concatenate strings

Joins two or more strings together. Unlike many languages, XPath has no + operator for string concatenation — you must use concat(). It accepts any number of arguments and is commonly used to build composite labels or combine attribute values with text.

contains(str, sub)

Check if str contains sub

Returns true if the first string contains the second as a substring. This is case-sensitive. It's one of the most commonly used string functions — perfect for partial text matching when you don't know the exact content, like finding ingredients that mention 'pepper'.

starts-with(str, pre)

Check prefix

Returns true if the first string begins with the second string. This is useful for matching elements by the start of their content or attribute values — for example, finding IDs that start with a particular prefix, or elements whose text begins with a certain word.

substring(str, pos, len?)

Extract substring (1-based)

Extracts a portion of a string starting at the given position (1-based). The optional third argument specifies the length. If omitted, it returns everything from the start position onward. Note that XPath uses 1-based indexing, unlike most programming languages.

substring-before(str, delim)

Text before delimiter

Returns the part of the first string that appears before the first occurrence of the second string. If the delimiter isn't found, it returns an empty string. Useful for splitting strings — for example, getting the number part of '400g' by looking before 'g'.

substring-after(str, delim)

Text after delimiter

Returns everything after the first occurrence of the delimiter in the string. The counterpart to substring-before(). Together, they provide basic string splitting. For example, getting the unit from a measurement like '2 tbsp' by looking after the space.

string-length(str?)

Length of string

Returns the number of characters in a string. If no argument is given, it uses the string value of the context node. Useful in predicates to filter by text length — for example, finding elements with short or long names.

normalize-space(str?)

Trim and collapse whitespace

Strips leading and trailing whitespace, and replaces any internal sequence of whitespace characters with a single space. This is essential when comparing text content from XML, since formatting whitespace can otherwise cause unexpected mismatches.

translate(str, from, to)

Character-by-character replace

Replaces each character in 'from' with the corresponding character in 'to'. If 'to' is shorter, extra characters in 'from' are removed. This is XPath 1.0's only way to do case conversion — translate(name, 'ABC', 'abc') lowercases those letters. It works character by character, not on substrings.

← Back to full reference