package
DOM 3 XPath implemention and helper for node.js and the web
npm install xpathGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
181.1 kB
Includes dependencies.
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 xpathxpath is xml engine agnostic but we recommend xmldom:
npm install @xmldom/xmldomCan 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);
➡
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 Pottervar 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);➡
mynsvar 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 Pottervar 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 Pottervar 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. RowlingMIT
jlrishe
jrishe@gmail.com