xpath

WebJar for xpath

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

xpath
Last Version

Last Version

0.0.27
Release Date

Release Date

Type

Type

jar
Description

Description

xpath
WebJar for xpath
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/goto100/xpath

Download xpath

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/xpath/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>xpath</artifactId>
    <version>0.0.27</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/xpath/
implementation 'org.webjars.npm:xpath:0.0.27'
// https://jarcasting.com/artifacts/org.webjars.npm/xpath/
implementation ("org.webjars.npm:xpath:0.0.27")
'org.webjars.npm:xpath:jar:0.0.27'
<dependency org="org.webjars.npm" name="xpath" rev="0.0.27">
  <artifact name="xpath" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='xpath', version='0.0.27')
)
libraryDependencies += "org.webjars.npm" % "xpath" % "0.0.27"
[org.webjars.npm/xpath "0.0.27"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

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",            // 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);
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

Versions

Version
0.0.27