All Projects → ilinsky → Jquery Xpath

ilinsky / Jquery Xpath

jQuery XPath plugin (with full XPath 2.0 language support)

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Jquery Xpath

Pugixml
Light-weight, simple and fast XML parser for C++ with XPath support
Stars: ✭ 2,809 (+1523.7%)
Mutual labels:  xml, xpath, dom
Xml
XML without worries
Stars: ✭ 35 (-79.77%)
Mutual labels:  xml, xpath, dom
Jsoup
jsoup: the Java HTML parser, built for HTML editing, cleaning, scraping, and XSS safety.
Stars: ✭ 9,184 (+5208.67%)
Mutual labels:  dom, xml, xpath
Didom
Simple and fast HTML and XML parser
Stars: ✭ 1,939 (+1020.81%)
Mutual labels:  dom, xml, xpath
Domquery
PHP library for easy 'jQuery like' DOM traversing and manipulation.
Stars: ✭ 84 (-51.45%)
Mutual labels:  xpath, jquery, dom
Fluentdom
A fluent api for working with XML in PHP
Stars: ✭ 327 (+89.02%)
Mutual labels:  xml, xpath, dom
Nito
A jQuery library for building user interfaces
Stars: ✭ 56 (-67.63%)
Mutual labels:  jquery, dom
Onthefly
🔗 Generate TinySVG, HTML and CSS on the fly
Stars: ✭ 37 (-78.61%)
Mutual labels:  xml, dom
Internettools
XPath/XQuery 3.1 interpreter for Pascal with compatibility modes for XPath 2.0/XQuery 1.0/3.0, custom and JSONiq extensions, XML/HTML parsers and classes for HTTP/S requests
Stars: ✭ 82 (-52.6%)
Mutual labels:  xml, xpath
Preact Markup
⚡️ Render HTML5 as VDOM, with Components as Custom Elements!
Stars: ✭ 167 (-3.47%)
Mutual labels:  xml, dom
Markup
A Swift package for working with HTML, XML, and other markup languages, based on libxml2.
Stars: ✭ 93 (-46.24%)
Mutual labels:  xml, xpath
Dna.js
🧬 An uncomplicated user interface library for building data-driven semantic templates
Stars: ✭ 114 (-34.1%)
Mutual labels:  jquery, dom
Xqerl
Erlang XQuery 3.1 Processor
Stars: ✭ 44 (-74.57%)
Mutual labels:  xml, xpath
Xom
XOM™ is a new XML object model. It is an open source (LGPL), tree-based API for processing XML with Java that strives for correctness, simplicity, and performance, in that order.
Stars: ✭ 38 (-78.03%)
Mutual labels:  xml, xpath
Amplesdk
Ample SDK - JavaScript UI Framework
Stars: ✭ 169 (-2.31%)
Mutual labels:  xml, dom
Graphquery
GraphQuery is a query language and execution engine tied to any backend service.
Stars: ✭ 112 (-35.26%)
Mutual labels:  xml, xpath
Femtojs
femtoJS - Really small JavaScript (ES6) library for DOM manipulation.
Stars: ✭ 122 (-29.48%)
Mutual labels:  jquery, dom
Phpstamp
The XSL-way templating library for MS Office Word DOCX documents.
Stars: ✭ 150 (-13.29%)
Mutual labels:  xml, dom
Amazon Mobile Sentiment Analysis
Opinion mining of Mobile reviews on Amazon platform
Stars: ✭ 19 (-89.02%)
Mutual labels:  xml, xpath
Vent
jQuery inspired DOM events library
Stars: ✭ 30 (-82.66%)
Mutual labels:  jquery, dom

jQuery XPath plugin

This plugin is a fully featured XPath 2.0 query language implementation which can be used to query both HTML and XML documents in all web browsers. It uses the DOM-agnostic XPath 2.0 engine xpath.js originally developed for Ample SDK UI Framework.

Usage

Download and include jquery.xpath.js or jquery.xpath.min.js file on your page. Please be aware that the 'min' version does not have the detailed error messages that the jquery.xpath.js has but has been efficiently minimised to reduce its file size.

<script type="text/javascript" src="jquery.xpath.js"></script>

API Reference

jQuery XPath plugin comes with two easy to use entrance points:

  1. $(context).xpath(expression, resolver)
  2. $.xpath(context, expression, resolver)

In both cases the resolver function type parameter is optional and is only needed when the expression contains prefixes. In cases where the expression does not touch the document, the node type context parameter is not required.

Below are the sample queries.

Running queries with context

$(document).xpath("*"); // Returns {Element} html (direct child of context item - document)
$(document).xpath("//head << //body"); // Returns {Boolean} true (head is preceding body)
$(document).xpath("//*[parent::html][last()]") // Returns {Element} body (last child of html)
$(document.body).xpath("count(ancestor::node())"); // Returns {Number} 2 (2 ancestor nodes)
$(document.body).xpath("preceding-sibling::element()"); // Returns {Element} head (prev sibling)
$(document.documentElement).xpath("body | head"); // Returns {Element} head and body (ordered)
$(document.documentElement).xpath("body, head"); // Returns {Element} body and head (not ordered)

Running queries that do not require context

$().xpath("0.1+0.2"); // Returns {Number} 0.3 (Note: in JavaScript it returns 0.30000000000000004)
$().xpath("xs:date('2012-12-12')-xs:yearMonthDuration('P1Y1M')"); // Returns {String} '2011-11-12'
$().xpath("2 to 5"); // Returns {Number} 2, 3, 4 and 5
$().xpath("for $var in (1, 2, 3) return $var * 3"); // Returns {Number} 3, 6 and 9
$().xpath("round-half-to-even(35540, -2)"); // Returns {Number} 35500
$().xpath("translate('bar','abc','ABC')"); // Returns {String} BAr
$().xpath("matches('helloworld', 'hello world', 'x')"); // Returns {Boolean} true
$().xpath("xs:double('-INF') castable as xs:decimal)"); // Returns {Boolean} false
$().xpath("1e2 instance of xs:double"); //  Returns {Boolean} true
$().xpath("1.5 cast as xs:integer"); // Returns {Number} 1

Running queries with prefixes

$(document).xpath("//my:body", function(prefix) {
	if (prefix == "my")
		return "http://www.w3.org/1999/xhtml";
});	// Returns {Element} body ('my' prefix resolved to XHTML namespace)

Error reporting

Unlike browser's native XPath 1.0 processing which have very poor error reporting, the jQuery XPath plugin reports syntax and evaluation errors with a great level of detail. Provided that XPath expressions are not easy, it is extremely helpful to have good level of feedback from the processor.

Below are examples of the detailed error reporting.

Syntax errors

$().xpath("1 to "); // Throws "Error: Expected second operand in range expression"
$().xpath("$*"); // Throws "Error: Illegal use of wildcard in var expression variable name"
$(document).xpath("self::document()"); // Throws "Error: Unknown 'document' kind test"

Evaluation errors

$().xpath("1+'2'") // Throws "Error: Arithmetic operator is not defined for provided arguments"
$().xpath("self::node()"); // Throws "Error: In an axis step, the context item is not a node."
$().xpath("max((1,'2'))"); // Throws "Error: Input to max() contains a mix of not comparable values"

XPath 2.0 trace() function

trace is a very helpful XPath 2.0 function, that will let you print the result of the sub-expression during its evaluation right into the browser console log. Function trace requires 2 arguments: first - any type, second - string, it prints its arguments to the console and returns the first argument to the evaluator.

$().xpath("for $a in (1, 2), $b in (3 to 4) return trace($b, 'b: ') - $a"); // See browser console

Bear in mind that the items reported will either have a type of nodes, or internal XML Schema data types ;)

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].