XPath: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
Henrik (talk | contribs)
→‎More information: Added link to XPath tutorial.
No edit summary
 
(17 intermediate revisions by 6 users not shown)
Line 1: Line 1:
'''XPath''' (XML Path Language) is a language for addressing elements in a XML or HTML document. XPath expressions describe paths in a tree represention of the document.
{{stub}}
 
'''XPath''' (XML Path Language) is a language for addressing elements in a XML or HTML document. [[XPathExpression|XPathExpressions]] describe paths in a tree represention of the document.


XPath is very fast compared to manual DOM traversal (see e.g. [http://ejohn.org/blog/getelementsbyclassname-speed-comparison/ getElementsByClassName Speed Comparison]).
XPath is very fast compared to manual DOM traversal (see e.g. [http://ejohn.org/blog/getelementsbyclassname-speed-comparison/ getElementsByClassName Speed Comparison]).


This page is intended for tips and gotchas related to using XPath in Greasemonkey scripts. See the links below for documentation of the XPath language.
This page is intended for tips and gotchas related to using XPath in Greasemonkey scripts, not complete documentation. See the links below for that.


== Using XPath in Greasemonkey ==
== Using XPath in Greasemonkey ==


The most convenient way to use XPath in Greasemonkey scripts is with a [[Code_snippets#XPath_helper|helper function]]. The insides of that function illustrate the less convenient way.
The most convenient way to use XPath in Greasemonkey scripts is with a [[XPath_Helper|helper function]].
The insides of that function illustrate the less convenient way.
The native method is through the <code>document.evaluate</code> function.
 
== XPathResult resultTypes ==
 
* [[XPathResult#resultType_Constants|resultType Constants]]
* [http://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript#XPathResult_Defined_Constants Further reading at Mozilla Developer Center]


== Relative paths ==
== Relative paths ==


Note that relative XPath expressions, that start from some specific node, do not begin with a <code>/</code>. Start them with a period, e.g. <code>.//a</code>, or an axis, e.g. <code>descendant::a</code>. The default axis is <code>child</code>, so just <code>a</code> works to find an immediate descendant.
Note that relative [[XPathExpression|XPathExpressions]], that start from some specific node, do not begin with a <code>/</code>. Start them with a period, e.g. <code>.//a</code>, or an axis, e.g. <code>descendant::a</code>. The default axis is <code>child</code>, so just <code>a</code> works to find an immediate descendant.


== More information ==
== Links ==


* [http://developer.mozilla.org/en/docs/Introduction_to_using_XPath_in_JavaScript Mozilla Developer Center: Introduction to using XPath in JavaScript]
* [http://developer.mozilla.org/en/docs/Introduction_to_using_XPath_in_JavaScript Mozilla Developer Center: Introduction to using XPath in JavaScript]
* [http://developer.mozilla.org/en/docs/XPath:Axes Mozilla Developer Center: XPath axes]
* [http://developer.mozilla.org/en/docs/XPath:Functions Mozilla Developer Center: XPath functions]
* [http://www.zvon.org/xxl/XPathTutorial/General/examples.html XPath tutorial]
* [http://www.zvon.org/xxl/XPathTutorial/General/examples.html XPath tutorial]
* [http://en.wikipedia.org/wiki/XPath Wikipedia: XPath]
* [http://en.wikipedia.org/wiki/XPath Wikipedia: XPath]
 
* [https://addons.mozilla.org/en-US/firefox/addon/1095 XPath Checker (Firefox addon)]
{{stub}}

Latest revision as of 19:48, 3 November 2017


XPath (XML Path Language) is a language for addressing elements in a XML or HTML document. XPathExpressions describe paths in a tree represention of the document.

XPath is very fast compared to manual DOM traversal (see e.g. getElementsByClassName Speed Comparison).

This page is intended for tips and gotchas related to using XPath in Greasemonkey scripts, not complete documentation. See the links below for that.

Using XPath in Greasemonkey

The most convenient way to use XPath in Greasemonkey scripts is with a helper function. The insides of that function illustrate the less convenient way. The native method is through the document.evaluate function.

XPathResult resultTypes

Relative paths

Note that relative XPathExpressions, that start from some specific node, do not begin with a /. Start them with a period, e.g. .//a, or an axis, e.g. descendant::a. The default axis is child, so just a works to find an immediate descendant.

Links