XPath Functions

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.

Basic Syntax

XPath expressions work on a tree structure of XML and return nodes or values depending on the query.

Basic Example

				
					<bookstore>
   <book>
      <title>XML Developer's Guide</title>
      <price>44.95</price>
   </book>
   <book>
      <title>Learn XPath</title>
      <price>39.95</price>
   </book>
</bookstore>

				
			

In the above XML, we can use XPath to select nodes. For instance, /bookstore/book/title selects all <title> nodes inside the <book> nodes.

Categories of XPath Functions

  • Node Functions
  • String Functions
  • Boolean Functions
  • Number Functions
  • Date Functions (for XPath 2.0 and higher)

Node Functions

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.

String Functions

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.

Boolean Functions

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.

Number Functions

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.

Date Functions (XPath 2.0+)

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 !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India