XPath (XML Path Language) is used to navigate through elements and attributes in an XML document. It’s essential for querying and selecting nodes in XML data structures. XPath functions allow developers to perform specific operations like string manipulations, mathematical calculations, and node selections.
XPath expressions work on a tree structure of XML and return nodes or values depending on the query.
XML Developer's Guide
44.95
Learn XPath
39.95
In the above XML, we can use XPath to select nodes. For instance, /bookstore/book/title
selects all <title>
nodes inside the <book>
nodes.
These functions help you work directly with nodes.
count()
: Returns the number of nodes in a node-set.
Example:
XPath: count(/bookstore/book)
This returns 2
, as there are two <book>
nodes.
position()
: Returns the position of the current node.
Example:
XPath: /bookstore/book[position()=1]/title
Output: XML Developer's Guide
(title of the first book).
last()
: Returns the position of the last node.
Example:
XPath: /bookstore/book[last()]/title
Output: Learn XPath
.
These functions help manipulate strings inside XML elements.
contains()
: Checks if a string contains a specific substring.
Example:
XPath: //book[contains(title, 'Learn')]
This returns the book element with the title containing ‘Learn’, i.e., <title>Learn XPath</title>
.
concat()
: Concatenates two or more strings.
Example:
XPath: concat('Price: ', /bookstore/book[1]/price)
Output: Price: 44.95
.
substring()
: Extracts a substring from a string.
Example:
XPath: substring(/bookstore/book[1]/title, 1, 3)
Output: XML
.
These functions return boolean values (true
or false
).
boolean()
: Converts an argument to a boolean.
Example:
XPath: boolean(/bookstore/book/title)
This returns true
if there are any <title>
nodes, otherwise false
.
not()
: Returns the opposite of the boolean value.
Example:
XPath: not(boolean(/bookstore/book/author))
This would return true
if no <author>
node exists.
Functions that help with number manipulation.
sum()
: Returns the sum of the numeric values in a node-set.
Example:
XPath: sum(/bookstore/book/price)
Output: 84.9
(i.e., 44.95 + 39.95).
round()
: Rounds a number to the nearest integer.
Example:
XPath: round(/bookstore/book[1]/price)
Output: 45
.
XPath 1.0 lacks date functions, but XPath 2.0 introduces them.
current-date()
: Returns the current date.
Example:
XPath: current-date()
Output: Returns the current date, e.g., 2024-09-06
.
year-from-date()
: Extracts the year from a date.
Example:
XPath: year-from-date(current-date())
Output: 2024
.
XPath functions provide powerful tools to query, manipulate, and filter XML data. From node navigation to string manipulation and mathematical operations, mastering these functions enables efficient XML document processing.This chapter has covered the essentials of XPath functions, with practical examples and output explanations. Using these, you can perform virtually any XML query or manipulation without needing to reference other resources. Happy coding !❤️