Skip to content

xpath.js - Open source XPath 2.0 implementation in JavaScript (DOM agnostic)

License

Notifications You must be signed in to change notification settings

ArunGust/xpath.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

------------------------------------
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;

About

xpath.js - Open source XPath 2.0 implementation in JavaScript (DOM agnostic)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 61.6%
  • HTML 37.7%
  • PHP 0.7%