XML - XPath Functions and Expressions (Advanced Level)

XPath (XML Path Language) is used to locate and select nodes from an XML document. Advanced XPath goes beyond simple element selection and allows complex searching, filtering, calculations, and data manipulation inside XML files.

1. XPath Expressions

An XPath expression is a statement used to select nodes or values from an XML document.

Basic Example:

/bookstore/book/title

This selects all title elements inside book elements.

Advanced XPath expressions allow conditions, functions, and logical operations.

Example:

/bookstore/book[price>500]

This selects books whose price is greater than 500.


2. Predicates

Predicates are conditions written inside square brackets []. They filter nodes.

Examples:

/books/book[1]

Selects the first book.

/books/book[last()]

Selects the last book.

/books/book[@category='science']

Selects books with category attribute equal to science.


3. XPath Functions

XPath provides built-in functions to work with strings, numbers, nodes, and Boolean values.

String Functions

These functions manipulate text data.

Examples:

contains(title,'XML')

Checks if title contains the word XML.

starts-with(name,'A')

Selects elements whose name starts with A.

string-length(title)

Returns the length of text.

normalize-space(title)

Removes extra spaces.


Numeric Functions

Used for calculations.

Examples:

sum(/books/book/price)

Returns total price of all books.

round(45.6)

Rounds number to nearest integer.

floor(45.9)

Returns lower integer.

ceiling(45.1)

Returns higher integer.


Boolean Functions

Used for logical evaluation.

Examples:

boolean(/books/book)

Returns true if book exists.

not(@available)

Selects elements where attribute available is missing.


4. Node Functions

These functions work with XML nodes.

Examples:

count(/books/book)

Counts number of book elements.

position()

Returns position of current node.

last()

Returns last node position.

name()

Returns element name.


5. Logical Operators

XPath supports logical comparisons.

Examples:

/books/book[price>300 and price<800]
/books/book[@category='IT' or @category='Science']

Operators used:

  • and

  • or

  • =

  • !=

  •  
  • <

  • =

  • <=


6. Wildcards in XPath

Wildcards allow flexible searching.

Examples:

*

Selects all elements.

@*

Selects all attributes.

node()

Selects any node.


7. Axes in XPath

Axes define relationships between nodes.

Common axes:

  • child:: selects children

  • parent:: selects parent

  • ancestor:: selects ancestors

  • descendant:: selects all children and subchildren

  • following-sibling:: selects next siblings

  • preceding-sibling:: selects previous siblings

Example:

/bookstore/book/ancestor::store

8. Combining XPath Expressions

Multiple conditions can be combined.

Example:

/books/book[@category='IT'][price>500][1]

This selects the first IT category book with price greater than 500.


9. Real-World Uses of Advanced XPath

Advanced XPath is widely used in:

  • XSLT transformations

  • XML data extraction

  • Web scraping

  • SOAP and Web Services

  • Automated testing tools


10. Advantages of Advanced XPath

  • Powerful XML searching

  • Reduces coding effort

  • Supports complex filtering

  • Works with many XML technologies

Advanced XPath makes XML data querying flexible, precise, and efficient for modern applications.