Using Built-in XPath Functions on XML Data
XPath provides a powerful set of built-in functions designed to
evaluate, manipulate, and extract data from XML documents. This article
explores how to utilize four essential XPath
functions—count(), concat(),
substring(), and normalize-space()—to handle
numeric calculations, string transformations, and text cleanup directly
within your XML queries.
Sample XML Document
To demonstrate these functions, consider the following
catalog.xml snippet:
<catalog>
<book id="bk101">
<title> XML Developer's Guide </title>
<author>Gambardella, Matthew</author>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
<book id="bk102">
<title>Midnight Rain</title>
<author>Ralls, Kim</author>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
</book>
</catalog>1. count(): Aggregating
Nodes
The count() function returns an integer representing the
total number of nodes matching a given node-set expression. It is
commonly used for pagination, validation, and determining list
sizes.
Syntax:
count(node-set)Usage Example:
count(/catalog/book)Result:
2Filtering Example: Find the number of books priced over $10.00:
count(/catalog/book[price > 10])Result:
1
2. concat(): Combining
Strings
The concat() function joins two or more strings together
into a single string. It accepts at least two string arguments and
concatenates them sequentially.
Syntax:
concat(string1, string2, string3, ...)Usage Example: Generate a formatted label combining the book title and its identifier:
concat(/catalog/book[1]/title, " (ID: ", /catalog/book[1]/@id, ")")Result:
" XML Developer's Guide (ID: bk101)"
3.
substring(): Extracting Portions of Text
The substring() function extracts a subset of characters
from a source string, based on a 1-based start index and an optional
length parameter.
Syntax:
substring(sourceString, startPosition, [length])Usage Example (Extract Year): Extract the four-digit year from the publication date:
substring(/catalog/book[1]/publish_date, 1, 4)Result:
"2000"Usage Example (Without Length): Extract everything after the first five characters:
substring(/catalog/book[1]/publish_date, 6)Result:
"10-01"
4.
normalize-space(): Cleaning Whitespace
The normalize-space() function removes leading and
trailing whitespace from a string and replaces all internal sequences of
whitespace characters (spaces, tabs, newlines) with a single space. If
no argument is passed, it operates on the context node.
Syntax:
normalize-space([string])Usage Example: Clean up the irregular spacing in the first book’s title:
normalize-space(/catalog/book[1]/title)Result:
"XML Developer's Guide"Predicate Example: Locate a node using cleaned text in conditional checks:
/catalog/book[normalize-space(title) = "XML Developer's Guide"]/@idResult:
bk101
Combining Functions for Complex Queries
XPath functions can be nested to perform advanced operations in a single expression. For example, to create a clean, standardized bibliographic entry containing normalized text and extracted dates:
concat(
normalize-space(/catalog/book[1]/author),
" published '",
normalize-space(/catalog/book[1]/title),
"' in ",
substring(/catalog/book[1]/publish_date, 1, 4)
)
Result:
"Gambardella, Matthew published 'XML Developer's Guide' in 2000"