XPath: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
No edit summary
m (cat redirect)
 
(22 intermediate revisions by 5 users not shown)
Line 1: Line 1:
XPath is a language for addressing parts of an XML document. It is a [http://www.w3.org/TR/xpath W3C recommendation].
Moved to [http://developer.mozilla.org/en/docs/Using_XPath here] (MDC).
 
[[Category:Redirects]]
This article describes Mozilla interfaces exposing XPath functionality to Javascript code. It doesn't teach XPath itself, if you're unfamiliar with this technology, please refer to [http://www.w3schools.com/xpath/ 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, while the second is a string defining an XPath expression.
 
Note that you shouldn't use this function if you expect to get a long list of results from it.
 
<pre>
// evaluate an XPath expression aExpression against a given DOM node aNode,
// returning the results as an array
// thanks wanderingstan at morethanwarm dot mail dot com
function evaluateXPath(aNode, aExpr) {
  var xpe = new XPathEvaluator();
  var result = xpe.evaluate(aExpr, aNode, xpe.createNSResolver(aNode.ownerDocument.documentElement),
                            0, null);
  var found = [];
  while (res = result.iterateNext())
    found.push(res);
  return found;
}
</pre>
 
==Sample Usage==
Assume we have the following XML document (see also [[How to Create a DOM tree]] and [[Reading and Writing XML to and from DOM trees]]) and <code>people</code> is the <code><people></code> element:
 
<pre>
<?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>
</pre>
 
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.
 
<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>
 
==Resources==
* [http://www.topxml.com/code/default.asp?p=3&id=v20021221025528 XPath Visualizer for Mozilla and Firefox]
* [http://forums.mozillazine.org/viewtopic.php?t=229106 Forum discussion on this topic]
 
[[Category:Example code]] [[Category:XML in Javascript]]

Latest revision as of 14:57, 8 November 2006

Moved to here (MDC).