{"id":27446950,"url":"https://github.com/ilinsky/jquery-xpath","last_synced_at":"2025-04-15T05:59:46.101Z","repository":{"id":5900083,"uuid":"7118729","full_name":"ilinsky/jquery-xpath","owner":"ilinsky","description":"jQuery XPath plugin (with full XPath 2.0 language support)","archived":false,"fork":false,"pushed_at":"2022-03-21T20:41:30.000Z","size":238,"stargazers_count":180,"open_issues_count":5,"forks_count":64,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-04-13T00:22:59.244Z","etag":null,"topics":["dom","html","javascript","jquery","xml","xpath","xpath2"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ilinsky.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-12-11T20:23:35.000Z","updated_at":"2025-03-06T13:47:57.000Z","dependencies_parsed_at":"2022-08-31T00:02:00.178Z","dependency_job_id":null,"html_url":"https://github.com/ilinsky/jquery-xpath","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilinsky%2Fjquery-xpath","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilinsky%2Fjquery-xpath/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilinsky%2Fjquery-xpath/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilinsky%2Fjquery-xpath/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ilinsky","download_url":"https://codeload.github.com/ilinsky/jquery-xpath/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248964328,"owners_count":21190509,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dom","html","javascript","jquery","xml","xpath","xpath2"],"created_at":"2025-04-15T05:59:45.509Z","updated_at":"2025-04-15T05:59:46.095Z","avatar_url":"https://github.com/ilinsky.png","language":"JavaScript","readme":"jQuery XPath plugin\n=============\n\nThis 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.\nIt uses the DOM-agnostic XPath 2.0 engine [xpath.js](https://github.com/ilinsky/xpath.js) originally developed for [Ample SDK UI Framework](https://github.com/clientside/amplesdk).\n\nUsage\n-----------------\n\nDownload and include ` jquery.xpath.js ` or ` jquery.xpath.min.js ` file on your page.\nPlease 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.\n\n```html\n\u003cscript type=\"text/javascript\" src=\"jquery.xpath.js\"\u003e\u003c/script\u003e\n```\n\nAPI Reference\n-----------------\n\njQuery XPath plugin comes with two easy to use entrance points:\n\n1. ` $(context).xpath(expression, resolver) `\n2. ` $.xpath(context, expression, resolver) `\n\nIn both cases the `resolver` function type parameter is optional and is only needed when the expression contains prefixes.\nIn cases where the expression does not touch the document, the node type `context` parameter is not required.\n\nBelow are the sample queries.\n\n### Running queries with context ###\n\n```js\n$(document).xpath(\"*\"); // Returns {Element} html (direct child of context item - document)\n$(document).xpath(\"//head \u003c\u003c //body\"); // Returns {Boolean} true (head is preceding body)\n$(document).xpath(\"//*[parent::html][last()]\") // Returns {Element} body (last child of html)\n$(document.body).xpath(\"count(ancestor::node())\"); // Returns {Number} 2 (2 ancestor nodes)\n$(document.body).xpath(\"preceding-sibling::element()\"); // Returns {Element} head (prev sibling)\n$(document.documentElement).xpath(\"body | head\"); // Returns {Element} head and body (ordered)\n$(document.documentElement).xpath(\"body, head\"); // Returns {Element} body and head (not ordered)\n```\n\n### Running queries that do not require context ###\n\n```js\n$().xpath(\"0.1+0.2\"); // Returns {Number} 0.3 (Note: in JavaScript it returns 0.30000000000000004)\n$().xpath(\"xs:date('2012-12-12')-xs:yearMonthDuration('P1Y1M')\"); // Returns {String} '2011-11-12'\n$().xpath(\"2 to 5\"); // Returns {Number} 2, 3, 4 and 5\n$().xpath(\"for $var in (1, 2, 3) return $var * 3\"); // Returns {Number} 3, 6 and 9\n$().xpath(\"round-half-to-even(35540, -2)\"); // Returns {Number} 35500\n$().xpath(\"translate('bar','abc','ABC')\"); // Returns {String} BAr\n$().xpath(\"matches('helloworld', 'hello world', 'x')\"); // Returns {Boolean} true\n$().xpath(\"xs:double('-INF') castable as xs:decimal)\"); // Returns {Boolean} false\n$().xpath(\"1e2 instance of xs:double\"); //  Returns {Boolean} true\n$().xpath(\"1.5 cast as xs:integer\"); // Returns {Number} 1\n```\n\n### Running queries with prefixes ###\n\n```js\n$(document).xpath(\"//my:body\", function(prefix) {\n\tif (prefix == \"my\")\n\t\treturn \"http://www.w3.org/1999/xhtml\";\n});\t// Returns {Element} body ('my' prefix resolved to XHTML namespace)\n```\n\nError reporting\n-----------------\nUnlike 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.\nProvided that XPath expressions are not easy, it is extremely helpful to have good level of feedback from the processor.\n\nBelow are examples of the detailed error reporting.\n\n### Syntax errors ###\n```js\n$().xpath(\"1 to \"); // Throws \"Error: Expected second operand in range expression\"\n$().xpath(\"$*\"); // Throws \"Error: Illegal use of wildcard in var expression variable name\"\n$(document).xpath(\"self::document()\"); // Throws \"Error: Unknown 'document' kind test\"\n```\n\n### Evaluation errors ###\n\n```js\n$().xpath(\"1+'2'\") // Throws \"Error: Arithmetic operator is not defined for provided arguments\"\n$().xpath(\"self::node()\"); // Throws \"Error: In an axis step, the context item is not a node.\"\n$().xpath(\"max((1,'2'))\"); // Throws \"Error: Input to max() contains a mix of not comparable values\"\n```\n\n### XPath 2.0 trace() function ###\n\n``` trace ``` is a very helpful XPath 2.0 function, that will let you print the result of the sub-expression\nduring its evaluation right into the browser console log.\nFunction ``` trace ``` requires 2 arguments: first - any type, second - string, it prints its arguments to the console and returns the first argument to the evaluator.\n\n```js\n$().xpath(\"for $a in (1, 2), $b in (3 to 4) return trace($b, 'b: ') - $a\"); // See browser console\n```\n\nBear in mind that the items reported will either have a type of nodes, or internal XML Schema data types ;)\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filinsky%2Fjquery-xpath","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Filinsky%2Fjquery-xpath","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filinsky%2Fjquery-xpath/lists"}