XPath Nodes

Understanding node types and selection in XPath

🔷 What are XPath Nodes?

In XPath, everything in an XML document is a node. Understanding different node types helps you select exactly what you need: elements, attributes, text content, or even comments and processing instructions.


// Select different node types
var elements = "//book";        // Element nodes
var attributes = "//@id";       // Attribute nodes
var text = "//title/text()";    // Text nodes
                                    

Types of Nodes

XPath recognizes seven types of nodes in XML documents. Each type represents a different part of the document structure and requires specific selection methods.

📦

Element Nodes

XML tags and their content

//book
🏷️

Attribute Nodes

Element attributes

//@id
📝

Text Nodes

Text content inside elements

//title/text()
💬

Comment Nodes

XML comments

//comment()

🔹 Selecting Element Nodes

Element nodes are the most common type. Select them using element names:

// Sample XML
var xmlString = `<library>
    <book>
        <title>JavaScript Basics</title>
        <author>John Smith</author>
    </book>
    <book>
        <title>XML Guide</title>
        <author>Jane Doe</author>
    </book>
</library>`;

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

// Select all book elements
var result = xmlDoc.evaluate(
    "//book",
    xmlDoc,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
);

console.log("Found " + result.snapshotLength + " book elements");

Output:

Found 2 book elements

🔹 Selecting Attribute Nodes

Use @ symbol to select attributes:

// XML with attributes
var xmlString = `<library>
    <book id="B001" category="programming">
        <title>JavaScript</title>
    </book>
    <book id="B002" category="database">
        <title>SQL Basics</title>
    </book>
</library>`;

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

// Select all id attributes
var result = xmlDoc.evaluate(
    "//book/@id",
    xmlDoc,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
);

console.log("Book IDs:");
for (var i = 0; i < result.snapshotLength; i++) {
    console.log("- " + result.snapshotItem(i).value);
}

Output:

Book IDs:
- B001
- B002

🔹 Selecting Text Nodes

Use text() to get text content directly:

// Select text content of titles
var result = xmlDoc.evaluate(
    "//book/title/text()",
    xmlDoc,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
);

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

Output:

Book titles:
- JavaScript
- SQL Basics

🔹 Node Relationships

XPath understands parent, child, sibling, and ancestor relationships:

Relationship Axes:

  • child:: - Direct children (default axis)
  • parent:: - Parent node
  • ancestor:: - All ancestors
  • descendant:: - All descendants
  • following-sibling:: - Next siblings
  • preceding-sibling:: - Previous siblings
// Find parent of title element
var result = xmlDoc.evaluate(
    "//title/parent::book/@id",
    xmlDoc,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
);

console.log("Parent book IDs:");
for (var i = 0; i < result.snapshotLength; i++) {
    console.log("- " + result.snapshotItem(i).value);
}

Output:

Parent book IDs:
- B001
- B002

🔹 Selecting Multiple Node Types

Use the pipe (|) operator to select multiple node types:

// Select both title and author elements
var result = xmlDoc.evaluate(
    "//title | //author",
    xmlDoc,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null
);

console.log("Titles and Authors:");
for (var i = 0; i < result.snapshotLength; i++) {
    var node = result.snapshotItem(i);
    console.log(node.nodeName + ": " + node.textContent);
}

Output:

Titles and Authors:
title: JavaScript Basics
author: John Smith
title: XML Guide
author: Jane Doe

🔹 Node Position

Select nodes by their position using predicates:

// Select first book
var firstBook = xmlDoc.evaluate(
    "//book[1]/title/text()",
    xmlDoc,
    null,
    XPathResult.STRING_TYPE,
    null
);

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

console.log("First book: " + firstBook.stringValue);
console.log("Last book: " + lastBook.stringValue);

Output:

First book: JavaScript Basics
Last book: XML Guide

🧠 Test Your Knowledge

What symbol is used to select attribute nodes in XPath?