xpath.js

1.1.0 · abandoned · verified Sun Apr 19

xpath.js is a pure JavaScript implementation of the XPath 1.0 specification, designed for use in Node.js environments. Published as version 1.1.0, this library is XML engine agnostic, meaning it requires an external DOM parser (like `xmldom`) to process XML documents. The project's GitHub repository indicates its last commit was approximately five years ago, suggesting it is no longer actively maintained. As such, it primarily supports older Node.js versions and CommonJS modules. Its key differentiator was its pure JavaScript implementation, making it suitable for environments where native XML parsing capabilities were limited, but more modern and actively maintained XPath libraries exist today.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse an XML string using `xmldom` and then use `xpath.js` to select nodes, extract text content, and retrieve attribute values using XPath 1.0 expressions.

const select = require('xpath.js');
const dom = require('xmldom').DOMParser;

// Install xmldom: npm install xmldom

const xml = "<book><title>Harry Potter</title><author>J.K. Rowling</author></book>";
const doc = new dom().parseFromString(xml, 'text/xml');

// Select the title node
const titleNodes = select(doc, "//title");
console.log('Title node:', titleNodes[0].localName + ": " + titleNodes[0].firstChild.data);

// Get text value directly
const titleText = select(doc, "//title/text()")[0].data;
console.log('Title text:', titleText);

// Select an attribute value
const xmlWithAttr = "<book author='J. K. Rowling'><title>Harry Potter</title></book>";
const docWithAttr = new dom().parseFromString(xmlWithAttr, 'text/xml');
const author = select(docWithAttr, "/book/@author")[0].value;
console.log('Author attribute:', author);

view raw JSON →