Using XPath

  • Revision slug: Using_XPath
  • Revision title: Using XPath
  • Revision id: 95414
  • Created:
  • Creator: ericjung
  • Is current revision? No
  • Comment /* Wrapper function */

Revision Content

XPath is a language for addressing parts of an XML document. It is a W3C recommendation.

This article describes Mozilla interfaces exposing XPath functionality to JavaScript code. These are described in DOM Level 3 XPath (which is W3C Working Group Note at this moment).

This article does not attempt teach XPath itself. If you're unfamiliar with this technology, please refer to W3Schools XPath tutorial.

Wrapper function

The following function can be used to evaluate XPath expressions on given XML nodes. The first argument is a DOM node or Document object, while the second is a string defining an XPath expression.

// Evaluate an XPath expression aExpression against a given DOM node
// or Document object (aNode), returning the results as an array
// thanks wanderingstan at morethanwarm dot mail dot com for the
// initial work.
function evaluateXPath(aNode, aExpr) {
  var xpe = new XPathEvaluator();
  var nsResolver = xpe.createNSResolver(aNode.ownerDocument == null ?
    aNode.documentElement : aNode.ownerDocument.documentElement);
  var result = xpe.evaluate(aExpr, aNode, nsResolver, 0, null);
  var found = [];
  var res;
  while (res = result.iterateNext())
    found.push(res);
  return found;
}

This function uses new XPathEvaluator(). That constructor is specific to Mozilla. Scripts used on a webpage which might be used by other browsers should instead replace the call to new XPathEvaluator() with the following fragment:

  // XPathEvaluator is implemented on objects that implement Document
  var xpe = aNode.ownerDocument || aNode;

In that case the creation of the XPathNSResolver can be simplified as:

  var nsResolver = xpe.createNSResolver(xpe.documentElement);

Note however that createNSResolver should only be used if you are sure the namespace prefixes in the XPath expression match those in the document you want to query. Otherwise, you have to provide your own implementation of XPathNSResolver.

If you are using XMLHttpRequest to read a local or remote XML file into a DOM tree (as described in Parsing and serializing XML), the first argument to evaluateXPath() should be req.responseXML.

Sample usage

Assume we have the following XML document (see also How to Create a DOM tree and Parsing and serializing XML):

<?xml version="1.0"?>
<people>
  <person first-name="eric" middle-initial="H" last-name="jung">
    <address street="321 south st" city="denver" state="co" country="usa"/>
    <address street="123 main st" city="arlington" state="ma" country="usa"/>
  </person>

  <person first-name="jed" last-name="brown">
    <address street="321 north st" city="atlanta" state="ga" country="usa"/>
    <address street="123 west st" city="seattle" state="wa" country="usa"/>
    <address street="321 south avenue" city="denver" state="co" country="usa"/>
  </person>
</people>

You can now "query" the document with XPath expressions. Although walking the DOM tree can achieve similar results, using XPath expressions is much quicker and more powerful. If you can rely on id attributes, document.getElementById() is still powerful, but it's not nearly as powerful as XPath. Here are some examples.

// display the last names of all people in the doc
var results = evaluateXPath(people, "//person/@last-name");
for (var i in results)
  alert("Person #" + i + " has the last name " + results[i].value);

// get the 2nd person node
results = evaluateXPath(people, "/people/person[2]");

// get all the person nodes that have addresses in denver
results = evaluateXPath(people, "//person[address/@city='denver']");

// get all the addresses that have "south" in the street name
results = evaluateXPath(people,  "//address[contains(@street, 'south')]");
alert(results.length);

Resources

  • XPath
  • XPath Visualizer for Mozilla and Firefox
  • XPath tutorial
  • Forum discussion on this topic
  • Using the Mozilla JavaScript Interface to XPath - draft tutorial on using XPath from javascript
  • Sarissa - Sarissa is a cross-browser ECMAScript library for client side XML manipulation, including loading XML from URLs or strings, performing XSLT transformations, XPath queries and more. Supported: Gecko (Mozilla, Firefox etc), IE, KHTML (Konqueror, Safari). If you're writing JavaScript that is used in both XUL applications and HTML pages, and the HTML pages may be viewed in non-Gecko-based applications (such as Internet Explorer, Opera, Konqueror, Safari), you should consider using Sarissa to parse and/or serialize XML. Note: Do not create a DOM object using document.implementation.createDocument() and then use Sarissa classes and methods to manipulate that object. It will not work. You must use Sarissa to create the initial DOM object.

Revision Source

<p>
</p><p><a href="en/XPath">XPath</a> is a language for addressing parts of an XML document. It is a <a class="external" href="http://www.w3.org/TR/xpath">W3C recommendation</a>.
</p><p>This article describes Mozilla interfaces exposing XPath functionality to JavaScript code. These are described in <a class="external" href="http://www.w3.org/TR/DOM-Level-3-XPath/">DOM Level 3 XPath</a> (which is W3C Working Group Note at this moment).
</p><p>This article does not attempt teach XPath itself. If you're unfamiliar with this technology, please refer to <a class="external" href="http://www.w3schools.com/xpath/">W3Schools XPath tutorial</a>.
</p>
<h3 name="Wrapper_function">Wrapper function</h3>
<p>The following function can be used to evaluate XPath expressions on given XML nodes. The first argument is a DOM node or Document object, while the second is a string defining an XPath expression.
</p>
<pre>// Evaluate an XPath expression aExpression against a given DOM node
// or Document object (aNode), returning the results as an array
// thanks wanderingstan at morethanwarm dot mail dot com for the
// initial work.
function evaluateXPath(aNode, aExpr) {
  var xpe = new XPathEvaluator();
  var nsResolver = xpe.createNSResolver(aNode.ownerDocument == null ?
    aNode.documentElement : aNode.ownerDocument.documentElement);
  var result = xpe.evaluate(aExpr, aNode, nsResolver, 0, null);
  var found = [];
  var res;
  while (res = result.iterateNext())
    found.push(res);
  return found;
}
</pre>
<p>This function uses <code>new XPathEvaluator()</code>. That constructor is specific to Mozilla. Scripts used on a webpage which might be used by other browsers should instead replace the call to <code>new XPathEvaluator()</code> with the following fragment:
</p>
<pre>  // XPathEvaluator is implemented on objects that implement Document
  var xpe = aNode.ownerDocument || aNode;
</pre>
<p>In that case the creation of the <a href="en/DOM/document.createNSResolver">XPathNSResolver</a> can be simplified as:
</p>
<pre>  var nsResolver = xpe.createNSResolver(xpe.documentElement);
</pre>
<p>Note however that <code>createNSResolver</code> should only be used if you are sure the namespace prefixes in the XPath expression match those in the document you want to query. Otherwise, you have to provide your own implementation of XPathNSResolver.
</p><p>If you are using <a href="en/XMLHttpRequest">XMLHttpRequest</a> to read a local or remote XML file into a DOM tree (as described in <a href="en/Parsing_and_serializing_XML">Parsing and serializing XML</a>), the first argument to <code>evaluateXPath()</code> should be <code>req.responseXML</code>.
</p>
<h3 name="Sample_usage">Sample usage</h3>
<p>Assume we have the following XML document (see also <a href="en/How_to_Create_a_DOM_tree">How to Create a DOM tree</a> and <a href="en/Parsing_and_serializing_XML">Parsing and serializing XML</a>):
</p>
<pre>&lt;?xml version="1.0"?&gt;
&lt;people&gt;
  &lt;person first-name="eric" middle-initial="H" last-name="jung"&gt;
    &lt;address street="321 south st" city="denver" state="co" country="usa"/&gt;
    &lt;address street="123 main st" city="arlington" state="ma" country="usa"/&gt;
  &lt;/person&gt;

  &lt;person first-name="jed" last-name="brown"&gt;
    &lt;address street="321 north st" city="atlanta" state="ga" country="usa"/&gt;
    &lt;address street="123 west st" city="seattle" state="wa" country="usa"/&gt;
    &lt;address street="321 south avenue" city="denver" state="co" country="usa"/&gt;
  &lt;/person&gt;
&lt;/people&gt;
</pre>
<p>You can now "query" the document with XPath expressions. Although walking the DOM tree can achieve similar results, using XPath expressions is much quicker and more powerful. If you can rely on <code>id</code> attributes, <code>document.getElementById()</code> is still powerful, but it's not nearly as powerful as XPath. Here are some examples.
</p>
<pre>// display the last names of all people in the doc
var results = evaluateXPath(people, "//person/@last-name");
for (var i in results)
  alert("Person #" + i + " has the last name " + results[i].value);

// get the 2nd person node
results = evaluateXPath(people, "/people/person[2]");

// get all the person nodes that have addresses in denver
results = evaluateXPath(people, "//person[address/@city='denver']");

// get all the addresses that have "south" in the street name
results = evaluateXPath(people,  "//address[contains(@street, 'south')]");
alert(results.length);
</pre>
<h3 name="Resources">Resources</h3>
<ul><li> <a href="en/XPath">XPath</a>
</li><li> <a class="external" href="http://www.topxml.com/code/default.asp?p=3&amp;id=v20021221025528">XPath Visualizer for Mozilla and Firefox</a>
</li><li> <a class="external" href="http://www.w3schools.com/xpath/">XPath tutorial</a>
</li><li> <a class="external" href="http://forums.mozillazine.org/viewtopic.php?t=229106">Forum discussion on this topic</a>
</li><li> <a class="external" href="http://zeus.jesus.cam.ac.uk/~jg307/mozilla/xpath-tutorial.html">Using the Mozilla JavaScript Interface to XPath</a> - draft tutorial on using XPath from javascript
</li><li> <a class="external" href="https://sourceforge.net/projects/sarissa/">Sarissa</a> - Sarissa is a cross-browser ECMAScript library for client side XML manipulation, including loading XML from URLs or strings, performing XSLT transformations, XPath queries and more. Supported: Gecko (Mozilla, Firefox etc), IE, KHTML (Konqueror, Safari). If you're writing JavaScript that is used in both XUL applications and HTML pages, and the HTML pages may be viewed in non-Gecko-based applications (such as Internet Explorer, Opera, Konqueror, Safari), you should consider using Sarissa to parse and/or serialize XML. <i>Note:</i> Do not create a DOM object using <code>document.implementation.createDocument()</code> and then use Sarissa classes and methods to manipulate that object. It will not work. You must use Sarissa to create the initial DOM object.
</li></ul>
Revert to this revision