Skip to content
This repository has been archived by the owner on Jun 9, 2018. It is now read-only.

MartijnR/xpathjs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XPathJS

XPathJS is a pure JavaScript implementation of XPath 1.0 and DOM Level 3 XPath specifications.

Features

  • Works in all major browsers: IE8+, Firefox, Chrome, Safari, Opera
  • Supports XML namespaces!
  • No external dependencies, include just a single .js file
  • Regression tested against hundreds of unit test cases.
  • Works in pages served as both, text/html and application/xhtml+xml content types.
  • Benchmarked against other XPath implementations.

Getting Started

  1. Download dist/xpathjs.min.js file.

  2. Include xpathjs.min.js in the <head> of your HTML document. NOTE: Make sure HTML document is in strict mode i.e. it has a !DOCTYPE declaration at the top!

  3. Initialize XPathJS:

// bind XPath methods to document and window objects
// NOTE: This will overwrite native XPath implementation if it exists
XPathJS.bindDomLevel3XPath();
```
  1. You can now use XPath expressions to query the DOM:

var result = document.evaluate(
    '//ul/li/text()', // XPath expression
    document, // context node
    null, // namespace resolver
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE
);

// loop through results
for (var i = 0; i < result.snapshotLength; i++) {
    var node = result.snapshotItem(i);
    alert(node.nodeValue);
}
```

Take a look at some working examples to get a better idea of how to use XPathJS.

We would strongly recommend for you to take a look at the CAVEATS document to get a better understanding of XPathJS limitations.

More examples, configuration options, and caveat info coming soon...

Background

So how did XPathJS come to be? Well originally we were looking for an implementation of a cross-browser XForms solution in hopes to alleviate the pain and complexity that comes with creating normal HTML forms. Unfortunately, we found out that there is neither an XForms engine that is fully implemented, nor does it support all browsers. So we thought, ok let's try to build our own! Then we realized that XForms makes extensive use of XPath. And again, we could not find a fully functional cross-browser XPath implementation. Long story short, that's how XPathJS was born.

By releasing XPathJS, we hope to help promote the adoption of open standards in the community.

Development

XPathJS is developed by Andrej Pavlovic. You are more than welcome to contribute by logging issues, sending pull requests, or just giving feedback.

License

We have released XPathJS under the AGPLv3. Note that we sell commercial licenses as well. You will need to sign a contributor agreement when contributing code due to the dual-license nature of the project.

Alternatives

Here are some other javascript-based XPath alternatives out there:

Build

In order to build the code yourself, you will need the following tools:

Once the tools are installed, do the following:

git clone https://github.com/andrejpavlovic/xpathjs.git
cd xpathjs
npm install
grunt dist

About

Cross-browser XPath 1.0 implementation in JavaScript.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 95.9%
  • PHP 2.9%
  • HTML 1.1%
  • CSS 0.1%