Giter Club home page Giter Club logo

xpath's Introduction

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

xpath's People

Contributors

bbyars avatar boreplusplus avatar callicles avatar cjbarth avatar darobin avatar delibes avatar deskoh avatar eugenelim avatar gdenat avatar gngeorgiev avatar goto100 avatar ideahunter avatar ivandimanov avatar jkphl avatar jlrishe avatar joepie91 avatar kenjibito avatar mernen avatar mramos-dev avatar shadiakiki1986 avatar teknopaul avatar thomasweinert avatar totopsy avatar vzvu3k6k avatar yaronn avatar yowkah avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.