What is wicked-good-xpath?
The wicked-good-xpath npm package is a JavaScript library that provides an implementation of XPath 1.0 for use in web browsers and Node.js. It allows you to query and manipulate XML documents using XPath expressions.
What are wicked-good-xpath's main functionalities?
Evaluate XPath Expressions
This feature allows you to evaluate XPath expressions against an XML document. The code sample demonstrates how to install the XPath evaluator, parse an XML string, and evaluate an XPath expression to retrieve a specific element.
const wgxpath = require('wicked-good-xpath');
const { DOMParser } = require('xmldom');
// Install wgxpath
wgxpath.install();
// Sample XML
const xml = '<root><element id="1">Value</element></root>';
const doc = new DOMParser().parseFromString(xml, 'text/xml');
// Evaluate XPath expression
const result = doc.evaluate('//element[@id="1"]', doc, null, wgxpath.XPathResultType.ANY_TYPE, null);
const node = result.iterateNext();
console.log(node.textContent); // Output: Value
Install XPath in the Global Scope
This feature allows you to install the XPath evaluator globally, making it available throughout your application without needing to import it in every module.
const wgxpath = require('wicked-good-xpath');
// Install wgxpath globally
wgxpath.install();
// Now you can use XPath expressions globally in your application
Other packages similar to wicked-good-xpath
xpath
The 'xpath' npm package is a pure JavaScript implementation of XPath 1.0. It is similar to wicked-good-xpath in that it allows you to evaluate XPath expressions against XML documents. However, 'xpath' is often used in server-side Node.js environments, whereas wicked-good-xpath is designed to work both in the browser and Node.js.
xml2js
The 'xml2js' npm package is a library for parsing XML to JavaScript objects and vice versa. While it does not provide XPath support directly, it can be used in conjunction with other libraries like 'xpath' to achieve similar functionality. It is more focused on XML parsing and manipulation rather than XPath evaluation.
xmldom
The 'xmldom' npm package is a DOM parser and serializer for XML. It provides a way to parse XML strings into DOM objects and serialize DOM objects back into XML strings. While it does not provide XPath support directly, it can be used as a foundation for libraries like 'xpath' and 'wicked-good-xpath' to evaluate XPath expressions.
Wicked Good XPath
About
Wicked Good XPath is a Google-authored pure JavaScript implementation of the DOM Level 3 XPath specification. It enables XPath evaluation for HTML documents in every browser. We believe it to be the fastest XPath implementation available in JavaScript.
Instructions
Download the latest wgxpath.install.js file and include it on your webpage with a script tag. For example:
<script src="wgxpath.install.js"></script>
Then call wgxpath.install()
from your JavaScript code, which will ensure document.evaluate
, the XPath evaluation function, is defined on the window object. To install the library on a different window, pass that window as an argument to the install function.
We provide an NPM package at https://www.npmjs.com/package/wicked-good-xpath.
There's also another NPM package at https://www.npmjs.com/package/wgxpath.
Building it Yourself
We use Gulp:
npm install
gulp
You can also run src/compile.sh
if you want to use different versions of
Closure Compiler / Closure Library.
History
Wicked Good XPath started as a Google Closure port of the JavaScript-XPath project by Cybozu Labs. At the time, JavaScript-XPath was the fastest JavaScript implementation of XPath available --- a whopping 10 times faster than Google's own AJAXSLT --- which made it a popular choice, notable for frontend web testing tools like Selenium and Web Puppeteer.
While it was fast, the code fell out of maintenance (last update was in 2007) so bugs were tough to get fixed. Also, since it wasn't written in Google Closure, it was tricky for us Googlers to integrate into our JavaScript applications. A rewrite was necessary.
However, we went beyond merely porting the library to Google Closure and fixing a couple bugs. We identified some significant additional performance improvements, such that our version runs about 30% faster than the original. On top of that, the Closure compiler was able to minify our code down to a mere 25K, 40% smaller than JavaScript-XPath's 42K (though it has grown a bit since). Finally, the code is structured and documented in a way that we believe will make future maintenance quicker and easier.