
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
DOM 3 XPath 1.0 implemention and helper for JavaScript, with node.js support.
Originally written by Cameron McCormack (blog).
Additional contributions from
Yaron Naveh (blog)
goto100
Thomas Weinert
Jimmy Rishe
and others
Install with npm:
npm install xpath
xpath is xml engine agnostic but we recommend xmldom:
npm install @xmldom/xmldom
Can be found here. See below for example usage.
var xpath = require('xpath');
var dom = require('@xmldom/xmldom').DOMParser;
var xml = "<book><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml, 'text/xml');
var nodes = xpath.select("//title", doc);
console.log(nodes[0].localName + ": " + nodes[0].firstChild.data);
console.log("Node: " + nodes[0].toString());
➡
title: Harry Potter
Node: <title>Harry Potter</title>
Using the same interface you have on modern browsers (MDN)
var node = null;
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml, 'text/xml');
var result = xpath.evaluate(
"/book/title", // xpathExpression
doc, // contextNode
null, // namespaceResolver
xpath.XPathResult.ANY_TYPE, // resultType
null // result
);
node = result.iterateNext();
while (node) {
console.log(node.localName + ": " + node.firstChild.data);
console.log("Node: " + node.toString());
node = result.iterateNext();
}
➡
title: Harry Potter
Node: <title>Harry Potter</title>
var xml = "<book><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml, 'text/xml');
var title = xpath.select("string(//title)", doc);
console.log(title);
➡
Harry Potter
var xml = "<book><title xmlns='myns'>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml, 'text/xml');
var node = xpath.select("//*[local-name(.)='title' and namespace-uri(.)='myns']", doc)[0];
console.log(node.namespaceURI);
➡
myns
var xml = "<book xmlns:bookml='http://example.com/book'><bookml:title>Harry Potter</bookml:title></book>"
var select = xpath.useNamespaces({"bookml": "http://example.com/book"});
console.log(select('//bookml:title/text()', doc)[0].nodeValue);
➡
Harry Potter
var xml = "<book xmlns='http://example.com/book'><title>Harry Potter</title></book>"
var select = xpath.useNamespaces({"bookml": "http://example.com/book"});
console.log(select('//bookml:title/text()', doc)[0].nodeValue);
➡
Harry Potter
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml, 'text/xml');
var author = xpath.select1("/book/@author", doc).value;
console.log(author);
➡
J. K. Rowling
MIT
Cheerio is a fast, flexible, and lean implementation of core jQuery designed specifically for the server. It allows for manipulation of the DOM using a familiar jQuery-like API. Cheerio parses markup and provides an API for traversing/manipulating the resulting data structure; it does not interpret XPath but uses CSS selectors instead.
jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards, for use with Node.js. It is more of a complete browser environment simulator that can also execute scripts, unlike xpath which is focused solely on XPath queries.
xml2js is a simple XML to JavaScript object converter. It supports bi-directional conversion. Unlike xpath, which is used for querying XML documents, xml2js is used for parsing XML into JavaScript objects and vice versa.
FAQs
DOM 3 XPath implemention and helper for node.js and the web
The npm package xpath receives a total of 5,871,722 weekly downloads. As such, xpath popularity was classified as popular.
We found that xpath demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.