All Projects → ilinsky → xpath2.js

ilinsky / xpath2.js

Licence: MIT license
xpath.js - Open source XPath 2.0 implementation in JavaScript (DOM agnostic)

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to xpath2.js

Didom
Simple and fast HTML and XML parser
Stars: ✭ 1,939 (+2520.27%)
Mutual labels:  dom, xpath
ElementFinder
Fetch data from HTML and XML via xpath/css and prepare it with regexp
Stars: ✭ 29 (-60.81%)
Mutual labels:  dom, xpath
XPath2.Net
Lightweight XPath2 for .NET
Stars: ✭ 26 (-64.86%)
Mutual labels:  xpath, xpath2
Jquery Xpath
jQuery XPath plugin (with full XPath 2.0 language support)
Stars: ✭ 173 (+133.78%)
Mutual labels:  dom, xpath
Jsoup
jsoup: the Java HTML parser, built for HTML editing, cleaning, scraping, and XSS safety.
Stars: ✭ 9,184 (+12310.81%)
Mutual labels:  dom, xpath
Fluentdom
A fluent api for working with XML in PHP
Stars: ✭ 327 (+341.89%)
Mutual labels:  dom, xpath
go-xmldom
XML DOM processing for Golang, supports xpath query
Stars: ✭ 38 (-48.65%)
Mutual labels:  dom, xpath
Xml
XML without worries
Stars: ✭ 35 (-52.7%)
Mutual labels:  dom, xpath
Domquery
PHP library for easy 'jQuery like' DOM traversing and manipulation.
Stars: ✭ 84 (+13.51%)
Mutual labels:  dom, xpath
Pugixml
Light-weight, simple and fast XML parser for C++ with XPath support
Stars: ✭ 2,809 (+3695.95%)
Mutual labels:  dom, xpath
affiliate
Add affiliation tags to links automatically in the browser
Stars: ✭ 77 (+4.05%)
Mutual labels:  dom
ready
Detect element availability on the initial page load and those dynamically appended to the DOM
Stars: ✭ 77 (+4.05%)
Mutual labels:  dom
front-end-interview-questions
No description or website provided.
Stars: ✭ 27 (-63.51%)
Mutual labels:  dom
fs2-data
streaming data parsing and transformation library
Stars: ✭ 103 (+39.19%)
Mutual labels:  xpath
vanilla-caret-js
Set and get Caret position (contenteditable or TextArea) using Vanilla JavaScript
Stars: ✭ 31 (-58.11%)
Mutual labels:  dom
respo.cljs
A virtual DOM library built with ClojureScript, inspired by React and Reagent.
Stars: ✭ 232 (+213.51%)
Mutual labels:  dom
dom
Package for access and manipulate DOM element in HTML file
Stars: ✭ 29 (-60.81%)
Mutual labels:  dom
playwright-demos
playwright for scrapping and UI testing / automate testing workflows
Stars: ✭ 65 (-12.16%)
Mutual labels:  dom
universal
A counterpart to common package to be used with Angular Universal
Stars: ✭ 115 (+55.41%)
Mutual labels:  dom
dom-to-image-more
Generates an image from a DOM node using HTML5 canvas
Stars: ✭ 231 (+212.16%)
Mutual labels:  dom

xpath2.js - Pure JavaScript implementation of XPath 2 query language

NodeJS workflow

About

xpath2.js is a DOM-agnostic open-source XPath 2.0 implementation in JavaScript. Execution engine operates using XML Schema 1.1 data types as prescribed by specification.

Features

  • Full XPath 2.0 language support
  • Arbitrary tree structure querying with XPath 2.0 language via custom DOMAdapter
  • Custom collation support (using StaticContext)
  • Custom function support (using StaticContext)
  • Variable injection (using DynamicContext)

Installation

npm install xpath2.js

Usage

The simple API implementation lib/index.js provided for reference. Its primary purpose is to demonstrate implementation classes wiring and a simple usable solution.

Basic scenarious with evaluate function

xpath.evaluate(expression, evaluationContext, staticContext, initialScope, DOMAdapter)

Parameters list

Name Type Required Description
expression String Required xpath expression
evaluationContext Variant Optional evaluation context (document, for example)
staticContext StaticContext or Function Optional compilation context or namespace resolver
initialScope Object Optional JavaScript variable values map
DOMAdapter DOMAdapter Optional document object model adapter

Query without a context

const xpath = require("xpath2.js");
const result = xpath.evaluate("1 to 5");
console.log(result); // prints [ 1, 2, 3, 4, 5 ]

Query a document not specifying namespaces

const xpath = require("xpath2.js");
const xmldom = require("xmldom"); // You are free to use any DOM implementation
const document = new xmldom.DOMParser().parseFromString('<test>content</test>');

const result = xpath.evaluate("fn:string(/test/text())", document);
console.log(result); // prints [ 'content' ]

Query a document with namespace resolver

Evaluating expressions over documents that specify namespaces requires namespace resolver to be provided with the query. Take a note that namespace resolver is there to resolve prefixes found in XPath expressions, thus making use of prefixes in expressions scoped to the query, and not to the document.

A namespace resolver is a function that takes single argument String prefix and returns a namespace uri for it.

Exception XPST0081 will be thrown, should any of the prefixes used in expression are left unresolved.

const xpath = require("xpath2.js");
const xmldom = require("xmldom");
const document = new xmldom.DOMParser().parseFromString('<foo><a:bar xmlns:a="http://a">content</a:bar></foo>');
const namespaceResolver = function(prefix) {
    if (prefix == "b")
        return "http://a";
    return null;
};

const result = xpath.evaluate("fn:string(//b:bar/text())", document, namespaceResolver);
console.log(result); // prints [ 'content' ]

Passing a JavaScript variable to the evaluation context

const xpath = require("xpath2.js");

const result = xpath.evaluate("$a + 0.2", null, null, {a: 0.1});
console.log(result); // prints [ 0.3 ]

More challenging scenarious

Using execute function and managing contexts

const xpath = require("xpath2.js");
const xmldom = require("xmldom");
const document = new xmldom.DOMParser().parseFromString('<foo><a:bar xmlns:a="http://a">content</a:bar></foo>');
const namespaceResolver = function(prefix) {
    if (prefix == "b")
        return "http://a";
    return null;
};
const staticContext = xpath.createStaticContext(namespaceResolver);
// Set default function namespace to the one of XPath functions, so "fn" prefix can be dropped in queries
staticContext.defaultFunctionNamespace = "http://www.w3.org/2005/xpath-functions";
const dynamicContext = xpath.createDynamicContext(staticContext, document);
const expression = xpath.compile("string(//b:bar/text())", staticContext);

const result = xpath.execute(expression, dynamicContext);
console.log(result); // prints [ 'content' ]

Note! Dynamic context carries date/time obtained during its creation

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].