xpath.js
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
-
ReferenceError: require is not defined
cause Attempting to use `require()` in a JavaScript module (ESM) context or a browser without a CommonJS loader.fixEnsure your environment is set up for CommonJS modules, or use a bundler (like Webpack) to transpile the code for ESM. For native Node.js ESM, this package is not directly compatible. Alternatively, change your file extension to `.cjs` if using Node.js modules. -
TypeError: Cannot read properties of undefined (reading 'localName')
cause This error typically occurs when an XPath expression doesn't match any nodes, resulting in an empty array or `undefined` being accessed without a check.fixAlways check if the result array from `select()` is not empty and contains the expected elements before attempting to access properties like `[0].localName` or `[0].data`. For example: `const nodes = select(doc, '//nonexistent'); if (nodes.length > 0) { console.log(nodes[0].localName); }` -
Error: Document not an instance of Node
cause `xpath.js` expects a DOM `Node` object (typically an `Element` or `Document`) as its first argument. This error indicates that the provided argument is not a valid Node instance, often because the XML parsing failed or an incorrect object was passed.fixEnsure that the XML document is successfully parsed into a DOM structure using a library like `xmldom` before passing it to `xpath.js`. Verify that `new dom().parseFromString(xml, 'text/xml')` returns a valid document object.
Warnings
- breaking This package is an older CommonJS module (v1.1.0) and does not support native ES Modules (ESM) syntax (`import ... from '...'`) without transpilation. Directly using `import` statements in a native ESM context will result in a runtime error.
- gotcha The `xpath.js` library is based on XPath 1.0, which means it lacks many features introduced in XPath 2.0 and 3.x, such as advanced data types, sequences, user-defined functions, and JSON support.
- breaking The `xpath.js` GitHub repository has not seen activity in approximately five years, indicating the package is abandoned. This means there will be no new features, bug fixes, or security updates, potentially leading to compatibility issues with newer Node.js versions or unaddressed vulnerabilities.
- gotcha Using XPath to query user-supplied XML or allowing user input directly into XPath expressions without proper sanitization can lead to XPath Injection vulnerabilities. This is analogous to SQL Injection and can allow attackers to read unauthorized data or manipulate application logic.
Install
-
npm install xpath.js -
yarn add xpath.js -
pnpm add xpath.js
Imports
- select
import { select } from 'xpath.js';import select from 'xpath.js'; // Requires transpilation or CommonJS fallback const select = require('xpath.js');
Quickstart
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);