What is xpath?
The xpath npm package is a Node.js module that allows users to query XML documents using XPath expressions. It provides a way to navigate and select nodes in an XML document tree, extract information, and manipulate the content.
What are xpath's main functionalities?
Evaluating XPath expressions
This feature allows you to evaluate XPath expressions against an XML document and returns the matching nodes.
const xpath = require('xpath'),
dom = require('xmldom').DOMParser;
let doc = new dom().parseFromString('<book><title>Harry Potter</title></book>');
let nodes = xpath.select('//title', doc);
console.log(nodes[0].textContent); // Harry Potter
Registering custom namespaces
This feature enables the use of custom namespaces when evaluating XPath expressions, which is necessary when querying XML documents that include namespaces.
const select = xpath.useNamespaces({
'x': 'http://www.w3.org/1999/xhtml'
});
let nodes = select('//x:div', doc);
Using XPath functions
This feature allows the use of XPath functions within the expression to perform operations like counting nodes, string manipulation, etc.
let result = xpath.select('count(//title)', doc);
console.log(result); // 1
Other packages similar to xpath
cheerio
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
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
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.
xpath
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
Install with npm:
npm install xpath
xpath is xml engine agnostic but I recommend to use xmldom:
npm install xmldom
API Documentation
Can be found here. See below for example usage.
Your first xpath:
var xpath = require('xpath')
, dom = require('xmldom').DOMParser
var xml = "<book><title>Harry Potter</title></book>"
var doc = new dom().parseFromString(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>
Alternatively
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)
var result = xpath.evaluate(
"/book/title",
doc,
null,
xpath.XPathResult.ANY_TYPE,
null
)
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>
Evaluate string values directly:
var xml = "<book><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml);
var title = xpath.select("string(//title)", doc);
console.log(title);
➡
Harry Potter
Namespaces
var xml = "<book><title xmlns='myns'>Harry Potter</title></book>"
var doc = new dom().parseFromString(xml)
var node = xpath.select("//*[local-name(.)='title' and namespace-uri(.)='myns']", doc)[0]
console.log(node.namespaceURI)
➡
myns
Namespaces with easy mappings
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
Default namespace with mapping
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
Attributes
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>"
var doc = new dom().parseFromString(xml)
var author = xpath.select1("/book/@author", doc).value
console.log(author)
➡
J. K. Rowling
License
MIT