0.0.34 • Published 3 months ago

xpath v0.0.34

Weekly downloads
791,766
License
MIT
Repository
github
Last release
3 months ago

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 we recommend xmldom:

npm install @xmldom/xmldom

API Documentation

Can be found here. See below for example usage.

Your first xpath:

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>

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, '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>

Evaluate string values directly:

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

Namespaces

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

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, 'text/xml');
var author = xpath.select1("/book/@author", doc).value;

console.log(author);

J. K. Rowling

License

MIT

@wemystic/esp-gatewayethos-themedweevediscordturkeywfs2swaggerurl-xiloadfluxpitstop-server-cliweather-rest@nowams/verovioscoreeditormimosa-svgstoreproxyclick-clirapid-crawlernami-utilsmba-parser@ionic/e2efindxooshienode-amcdocx-node@kukefess/kkeditor-mathmlboulevardier-schematicreact-native-slider-kf@twbs/svg-spritesfdx-hardisalexandria-amr-to-eventbridgesaml-update@everything-registry/sub-chunk-3187libsamlkaaviolgtv-2012hpcsahexo-plugin-socialshomebridge-http-securitysystemhexo-douban-prohexo-douban-up2homebridge-http-advanced-accessoryhomebridge-http-entryhomebridge-http-garagedoorhomebridge-openremotehexo-generator-tagsnethexo-butterfly-doubanhexo-butterfly-douban-zkeqhexo-douban-indigohexo-douban-listhexo-douban-list2hexo-douban-mellowhexo-douban-modifiedhexo-douban-modifyhexo-douban-musichexo-douban-plushtythttpyachttpqueryhttp2-serverpush-proxyhyperpotamusibm-ia-restibm-igc-extensionsibm-igc-lineageibm-iis-commonskoishi-plugin-stnbkousugp-boilerplate-webservergrunt-localizegrunt-flexpmdgraphql-request-slagrunt-bridgekuben-appium-ios-driverldmllatest-tweetslatest-tweets-modernlawtextlegispro-clilarvittmpliobroker.ronoluluiobroker.openknxiva-vxml-aetna-srcjaxinejeeebotloadrushnode-etl-fluentnode-nia-connectormx-appium-desktopmxbuildernip24clientnode-dynamicscrm-apimime-contentmainetcnmobile-build-helpermichael.bakker_xsd2jsonschemamoi-helpermonaco-xsd-code-completionmorpwxmountebank-devmountebank-distributedlifemountebank-forkmountebank-testmountebank-testsmountebank_fault_simulationmountebankmv-textract
0.0.34

3 months ago

0.0.33

9 months ago

0.0.32

3 years ago

0.0.31

3 years ago

0.0.30

3 years ago

0.0.29

4 years ago

0.0.28

4 years ago

0.0.27

6 years ago

0.0.26

6 years ago

0.0.25

6 years ago

0.0.24

7 years ago

0.0.23

8 years ago

0.0.22

8 years ago

0.0.21

8 years ago

0.0.20

8 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

12 years ago

0.0.4

12 years ago

0.0.2

12 years ago

0.0.1

12 years ago