xpath.js - Open source XPath 2.0 implementation in JavaScript (DOM agnostic)
JavaScript HTML PHP
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
api
res
src
test
.htaccess
COPYING
README.md
doc_helper.js
package.json
xpath.js
xpath.js_loader.js

README.md

------------------------------------
XPath.js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
------------------------------------
Copyright (c) 2012 Sergey Ilinsky
Dual licensed under the MIT and GPL
------------------------------------

About:

  • XPath.js is a DOM-agnostic open-source XPath 2.0 implementation in JavaScript
  • Library can be used to query any DOM structure via custom DOMAdapter
  • Internally engine operates on XML Schema 1.1 data types

Structure:

  • api/ - Sample API sources
  • src/ - XPath 2.0 engine sources
  • test/unit/ - unit.js tests (requires unit.js)

Usage:

  • Running on sources: include xpath.js API file from the root folder.
  • When no Apache/.htaccess/PHP configured, source files will be loaded by JS.

NodeJS (temporary solution):

using xpath.js on NodeJS:

//using xmldom as target document (https://github.com/jindw/xmldom)
var xpath=require("xpath2")(xmldom.domClasses.Document.prototype);

var test=xpath.evaluate("2 to 5");

a more sofisticated example

var xmldom=require("xmldom");
var fs=require("fs");
var DOMParser = xmldom.DOMParser;
var xpath=require("xpath.js")(xmldom.domClasses.Document.prototype);

function nodeName(e) {return e.nodeName;}
function nodeValue(e) {return e.nodeValue;}

var xml = new DOMParser().parseFromString(fs.readFileSync("data.xml").toString());
var xsl = new DOMParser().parseFromString(fs.readFileSync("content.xslt").toString());

xmldom.domClasses.Node.prototype.select=function(e) {
var oStaticContext=new xpath.classes.StaticContext();
oStaticContext.namespaceResolver=this.documentElement||this.ownerDocument.documentElement;
return xpath.evaluate(e,this,oStaticContext);
};

var repl = require("repl");
var r = repl.start("xpath2> ");

r.context.nodeName=nodeName;
r.context.nodeValue=nodeValue;
r.context.xmldom=xmldom;
r.context.xml=xml;
r.context.xsl=xsl;
r.context.xpath=xpath;