XPath Functions

Navigate and query XML documents efficiently

🧭 What is XPath?

XPath (XML Path Language) is a query language for selecting nodes from XML documents. It uses path expressions to navigate through elements and attributes, making data extraction simple and powerful.


// Select all book titles
/bookstore/book/title

// Select books with price > 30
//book[price>30]
                                    

XPath Function Categories

XPath provides built-in functions for string manipulation, numeric calculations, node operations, and boolean logic. These functions enable powerful queries and data transformations directly within XPath expressions.

🔤

String Functions

Manipulate text content

concat(), substring(), contains()
🔢

Numeric Functions

Perform calculations

sum(), count(), round()
📍

Node Functions

Work with nodes

name(), position(), last()

Boolean Functions

Logical operations

not(), true(), false()

🔹 Basic XPath Syntax

Understanding XPath path expressions:

<!-- Sample XML -->
<bookstore>
    <book category="fiction">
        <title>The Great Gatsby</title>
        <price>10.99</price>
    </book>
    <book category="science">
        <title>Physics 101</title>
        <price>35.00</price>
    </book>
</bookstore>
// Select all books
/bookstore/book

// Select all titles
//title

// Select first book
/bookstore/book[1]

// Select books with price > 20
//book[price>20]

// Select fiction books
//book[@category='fiction']

🔹 String Functions

Common string manipulation functions:

🔸 concat() - Combine Strings

// Combine first and last name
concat(firstname, ' ', lastname)

// Result: "John Doe"

🔸 substring() - Extract Text

// Get first 5 characters
substring(title, 1, 5)

// From "The Great Gatsby" → "The G"

🔸 contains() - Check Text

// Find books with "XML" in title
//book[contains(title, 'XML')]

🔸 string-length() - Get Length

// Get title length
string-length(title)

// "The Great Gatsby" → 16

🔸 starts-with() - Check Start

// Books starting with "The"
//book[starts-with(title, 'The')]

🔸 translate() - Replace Characters

// Convert to uppercase
translate(title, 'abc', 'ABC')

🔹 Numeric Functions

Functions for working with numbers:

🔸 count() - Count Nodes

// Count all books
count(//book)

// Count fiction books
count(//book[@category='fiction'])

🔸 sum() - Add Values

// Total price of all books
sum(//book/price)

// Result: 45.99

🔸 round() - Round Numbers

// Round price
round(price)

// 10.99 → 11

🔸 floor() and ceiling()

// Round down
floor(10.99)  // Result: 10

// Round up
ceiling(10.01)  // Result: 11

🔸 number() - Convert to Number

// Convert string to number
number('123')  // Result: 123

🔹 Node Functions

Functions for working with nodes:

🔸 position() - Get Position

// Select first 3 books
//book[position() <= 3]

// Select even-positioned books
//book[position() mod 2 = 0]

🔸 last() - Get Last Position

// Select last book
//book[last()]

// Select last 2 books
//book[position() > last()-2]

🔸 name() - Get Element Name

// Get element name
name(//book[1])  // Result: "book"

🔸 local-name() - Get Local Name

// Get name without namespace
local-name(//book[1])

🔹 Boolean Functions

Logical operations in XPath:

🔸 not() - Negate Condition

// Books NOT in fiction category
//book[not(@category='fiction')]

🔸 true() and false()

// Always true
//book[true()]

// Always false
//book[false()]

🔸 boolean() - Convert to Boolean

// Check if element exists
boolean(//book/author)

🔹 XPath Operators

Combine conditions with operators:

// AND operator
//book[price>20 and @category='fiction']

// OR operator
//book[price<15 or price>40]

// Comparison operators
//book[price = 10.99]
//book[price != 10.99]
//book[price < 20]
//book[price > 20]
//book[price <= 20]
//book[price >= 20]

// Math operators
//book[price + 5 > 30]
//book[price * 2 < 50]

🔹 XPath Axes

Navigate relationships between nodes:

// Child axis (default)
/bookstore/child::book

// Descendant axis
//descendant::title

// Parent axis
//title/parent::book

// Ancestor axis
//price/ancestor::bookstore

// Following-sibling
//book[1]/following-sibling::book

// Preceding-sibling
//book[2]/preceding-sibling::book

// Attribute axis
//book/attribute::category

🔹 Using XPath in JavaScript

Evaluate XPath expressions in the browser:

const xmlString = `
<bookstore>
    <book>
        <title>XML Guide</title>
        <price>29.99</price>
    </book>
</bookstore>
`;

const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "text/xml");

// Evaluate XPath
const result = xmlDoc.evaluate(
    "//book/title",
    xmlDoc,
    null,
    XPathResult.STRING_TYPE,
    null
);

console.log(result.stringValue); // Output: XML Guide

Output:

XML Guide

🔹 Advanced XPath Examples

Complex queries combining multiple functions:

// Books with titles longer than 10 characters
//book[string-length(title) > 10]

// Books in positions 2 to 4
//book[position() >= 2 and position() <= 4]

// Books with price between 20 and 40
//book[price >= 20 and price <= 40]

// Count expensive books
count(//book[price > 30])

// Average price (using sum and count)
sum(//book/price) div count(//book)

// Books with "Guide" in title (case-insensitive)
//book[contains(translate(title, 'GUIDE', 'guide'), 'guide')]

💡 XPath Best Practices:

  • Use specific paths instead of // when possible
  • Combine predicates for better performance
  • Use position() carefully with large datasets
  • Test XPath expressions with sample data
  • Consider using XPath 2.0 for advanced features

🧠 Test Your Knowledge

Which XPath function counts the number of nodes?