mozilla

Revision 110819 of Introduction to using XPath in JavaScript

  • Revision slug: Introduction_to_using_XPath_in_JavaScript
  • Revision title: Introduction to using XPath in JavaScript
  • Revision id: 110819
  • Created:
  • Creator: Jt
  • Is current revision? No
  • Comment

Revision Content

Introduction

This document describes the interface to access XPath functions from JavaScript.

Mozilla implements much of the DOM 3 XPath. This allows XPath expressions to be run against both HTML and XML documents.

The simplest interface to XPath is the evaluate function of the document object, which returns an object of type XPathResult:

var xpathResult = document.evaluate(xpathExpression, contextNode, namespaceResolver, resultType, result);

The evaluate function takes a total of five arguments:

  • xpathExpression: A string containing the XPath expression to be evaluated.
  • contextNode: A node in the document against which the XPath expression should be evaluated. The document node is the most commonly used.
  • namespaceResolver: A function that will be passed any namespace prefixes from xpathExpression and return a string representing the namespace URI associated with that prefix. This enables conversion between the prefixes used in the XPath expressions and the (possibly different) prefixes used in the document. The most commonly used value for this is null, which is used for HTML documents or when no namespace prefixes are used.
  • resultType: A numeric constant (or named constant properties, such as XPathResult.ANY_TYPE) that corresponds to the result of type XPathResult to return. (defined in the relevaant section of the XPath Spec). The most commonly passed constant is XPathResult.ANY_TYPE which will return the results of the XPath expression as the most natural type.
  • result: An existing XPathResult to use for the results. Passing null causes a new XPathResult to be created.


A Simple Example

To extract the level 2 headings of a HTML document using XPath the XPath Expression is simply //h2. The full code for this is then:

var headings = document.evaluate('//h2', document, null, XPathResult.ANY_TYPE, null);

Notice that, since HTML does not have namespaces, we have passed null for the namespaceResolver. Since we wish to search over the entire document for the headings, we have used the document object itself as the contextNode.

Revision Source

<h3 name="Introduction">Introduction</h3>
<p>This document describes the interface to access <a href="en/XPath">XPath</a> functions from JavaScript.
</p><p>Mozilla implements much of the <a class="external" href="http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html">DOM 3 XPath</a>. This allows XPath expressions to be run against both HTML and XML documents.
</p><p>The simplest interface to XPath is the <a href="en/DOM/document.evaluate">evaluate</a> function of the <a href="en/DOM/document">document</a> object, which returns an object of type <a href="en/XPathResult">XPathResult</a>:
</p>
<pre>var xpathResult = document.evaluate(xpathExpression, contextNode, namespaceResolver, resultType, result);
</pre>
<p>The evaluate function takes a total of five arguments:
</p>
<ul><li><code>xpathExpression</code>: A string containing the XPath expression to be evaluated.
</li></ul>
<ul><li><code>contextNode</code>: A node in the document against which the XPath expression should be evaluated. The <a href="en/DOM/document">document</a> node is the most commonly used.
</li></ul>
<ul><li><code>namespaceResolver</code>: A function that will be passed any namespace prefixes from <code>xpathExpression</code> and return a string representing the namespace URI associated with that prefix. This enables conversion between the prefixes used in the XPath expressions and the (possibly different) prefixes used in the document. The most commonly used value for this is <code>null</code>, which is used for HTML documents or when no namespace prefixes are used.
</li></ul>
<ul><li><code>resultType</code>: A numeric constant (or named constant properties, such as <code>XPathResult.ANY_TYPE</code>) that corresponds to the result of type <code>XPathResult</code> to return. (defined in the relevaant section of the XPath Spec). The most commonly passed constant is XPathResult.ANY_TYPE which will return the results of the XPath expression as the most natural type.
</li></ul>
<ul><li> <code>result</code>: An existing XPathResult to use for the results. Passing <code>null</code> causes a new XPathResult to be created.
</li></ul>
<p><br>
</p>
<h3 name="A_Simple_Example">A Simple Example</h3>
<p>To extract the level 2 headings of a HTML document using XPath the XPath Expression is simply <code>//h2</code>. The full code for this is then:
</p>
<pre>var headings = document.evaluate('//h2', document, null, XPathResult.ANY_TYPE, null);</pre>
<p>Notice that, since HTML does not have namespaces, we have passed <code>null</code> for the <code>namespaceResolver</code>. Since we wish to search over the entire document for the headings, we have used the <a href="en/DOM/document">document</a> object itself as the <code>contextNode</code>.
</p>
Revert to this revision