XPath Syntax

Master XPath expressions and operators

📖 XPath Syntax Guide

XPath syntax uses path expressions, predicates, and operators to select nodes. Learning the syntax patterns helps you write powerful queries to extract exactly the data you need from XML documents efficiently.


// Basic syntax patterns
/library/book              // Absolute path
//book                     // Anywhere in document
//book[@id='B001']         // With condition
//book[price<30]/title     // Filtered selection
                                    

XPath Syntax Components

XPath expressions are built from several key components. Understanding each part helps you construct precise queries for any XML structure you encounter.

🛣️

Path Expressions

Navigate through XML structure

/library/book/title
🔍

Predicates

Filter nodes with conditions

//book[price>20]
⚙️

Operators

Compare and combine conditions

//book[price<30 and @id]
🔧

Functions

Built-in helper functions

//book[position()=1]

🔹 Path Expressions

Path expressions define how to navigate through the XML tree:

Path Syntax:

  • / - Selects from root node
  • // - Selects nodes anywhere in document
  • . - Selects current node
  • .. - Selects parent of current node
  • @ - Selects attributes
// Sample XML
var xmlString = `<library>
    <section name="Fiction">
        <book><title>Novel A</title></book>
    </section>
    <section name="Science">
        <book><title>Science B</title></book>
    </section>
</library>`;

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

// Absolute path
var result1 = xmlDoc.evaluate("/library/section/book/title", xmlDoc, 
    null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

// Relative path (anywhere)
var result2 = xmlDoc.evaluate("//title", xmlDoc, 
    null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

console.log("Absolute path found: " + result1.snapshotLength);
console.log("Relative path found: " + result2.snapshotLength);

Output:

Absolute path found: 2
Relative path found: 2

🔹 Predicates (Filters)

Predicates filter nodes based on conditions. They're written in square brackets:

// XML with prices
var xmlString = `<store>
    <book><title>Book A</title><price>15</price></book>
    <book><title>Book B</title><price>25</price></book>
    <book><title>Book C</title><price>35</price></book>
</store>`;

var xmlDoc = parser.parseFromString(xmlString, "text/xml");

// Books with price less than 30
var result = xmlDoc.evaluate(
    "//book[price<30]/title/text()",
    xmlDoc,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
);

console.log("Affordable books:");
for (var i = 0; i < result.snapshotLength; i++) {
    console.log("- " + result.snapshotItem(i).nodeValue);
}

Output:

Affordable books:
- Book A
- Book B

🔹 XPath Operators

Operators let you create complex conditions:

Comparison Operators:

  • = - Equal to
  • != - Not equal to
  • < - Less than
  • > - Greater than
  • <= - Less than or equal
  • >= - Greater than or equal

Logical Operators:

  • and - Both conditions must be true
  • or - Either condition can be true
  • not() - Negates a condition
// XML with attributes and prices
var xmlString = `<store>
    <book id="B001"><title>JavaScript</title><price>25</price></book>
    <book id="B002"><title>Python</title><price>30</price></book>
    <book><title>Ruby</title><price>20</price></book>
</store>`;

var xmlDoc = parser.parseFromString(xmlString, "text/xml");

// Books with id AND price less than 30
var result = xmlDoc.evaluate(
    "//book[@id and price<30]/title/text()",
    xmlDoc,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
);

console.log("Books with ID and price < 30:");
for (var i = 0; i < result.snapshotLength; i++) {
    console.log("- " + result.snapshotItem(i).nodeValue);
}

Output:

Books with ID and price < 30:
- JavaScript

🔹 XPath Functions

Built-in functions provide powerful selection capabilities:

Common Functions:

  • position() - Position of node in node-set
  • last() - Last node in node-set
  • count() - Count nodes
  • contains() - Check if string contains substring
  • starts-with() - Check if string starts with substring
  • string-length() - Length of string
// Using position() function
var firstBook = xmlDoc.evaluate(
    "//book[position()=1]/title/text()",
    xmlDoc,
    null,
    XPathResult.STRING_TYPE,
    null
);

// Using last() function
var lastBook = xmlDoc.evaluate(
    "//book[last()]/title/text()",
    xmlDoc,
    null,
    XPathResult.STRING_TYPE,
    null
);

// Using contains() function
var pythonBook = xmlDoc.evaluate(
    "//book[contains(title, 'Python')]/title/text()",
    xmlDoc,
    null,
    XPathResult.STRING_TYPE,
    null
);

console.log("First: " + firstBook.stringValue);
console.log("Last: " + lastBook.stringValue);
console.log("Contains 'Python': " + pythonBook.stringValue);

Output:

First: JavaScript
Last: Ruby
Contains 'Python': Python

🔹 Wildcards

Use wildcards to match multiple elements:

Wildcard Symbols:

  • * - Matches any element node
  • @* - Matches any attribute node
  • node() - Matches any node of any kind
// Select all child elements of book
var result = xmlDoc.evaluate(
    "//book[1]/*",
    xmlDoc,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
);

console.log("First book has " + result.snapshotLength + " child elements:");
for (var i = 0; i < result.snapshotLength; i++) {
    var node = result.snapshotItem(i);
    console.log("- " + node.nodeName + ": " + node.textContent);
}

Output:

First book has 2 child elements:
- title: JavaScript
- price: 25

🔹 Complete Syntax Example

Combine all syntax elements for complex queries:

// Complex XML
var xmlString = `<bookstore>
    <book category="web" lang="en">
        <title>Learning JavaScript</title>
        <author>John Doe</author>
        <price>29.99</price>
    </book>
    <book category="web" lang="en">
        <title>Mastering CSS</title>
        <author>Jane Smith</author>
        <price>24.99</price>
    </book>
    <book category="database" lang="en">
        <title>SQL Fundamentals</title>
        <author>Bob Johnson</author>
        <price>34.99</price>
    </book>
</bookstore>`;

var xmlDoc = parser.parseFromString(xmlString, "text/xml");

// Complex query: Web books under $30 in English
var result = xmlDoc.evaluate(
    "//book[@category='web' and @lang='en' and price<30]/title/text()",
    xmlDoc,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
);

console.log("Web books under $30:");
for (var i = 0; i < result.snapshotLength; i++) {
    console.log("- " + result.snapshotItem(i).nodeValue);
}

Output:

Web books under $30:
- Learning JavaScript
- Mastering CSS

🧠 Test Your Knowledge

What does the wildcard "*" match in XPath?