Introduction to using XPath in JavaScript

  • Revision slug: Introduction_to_using_XPath_in_JavaScript
  • Revision title: Introduction to using XPath in JavaScript
  • Revision id: 110815
  • 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 an 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. null is common for HTML documents or when no namespace prefixes are used.

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>xpathExpression: A string containing an XPath expression to be evaluated.
</li></ul>
<ul><li>contextNode: 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>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. null is common for HTML documents or when no namespace prefixes are used.
</li></ul>
Revert to this revision