From MozillaZine Knowledge Base

asquella, why did you write 'Note that you shouldn't use this function if you expect to get a long list of results from it.' --grimholtz

That function copies query results to an array, taking additional memory and CPU cycles. I wouldn't use it at all, but for simple queries I think that simpler interface is worth the overhead. (Not that I've done any profiling.)
Related example (though not completely applicable): try opening JS Console when it has a lot of entries asqueella

So this page is actually a bit misleading in that it suggests that you can only use XPath with XML - infact it will work with any DOM tree. I'm also not sure that making use of a helper function actually helps people understand what's going on (but then I should finish my documentation...) --Jgraham 03:20, 20 Mar 2005 (PST)

DOM trees are in-memory representations of XML. They are one and the same, so I don't see how this can be misleading.
HTML is not XML but HTML has a DOM and XPath works with HTML Jgraham
I think you are confusing the Document Object Model Core specification with the Document Object Model HTML specification. The former defines interfaces such as Document, Node, Element, Attr, etc., while the latter defines interfaces such as HTMLDocument and HTMLElement. The Document Object Model XPath, which defines interfaces such as XPathEvaluator, XPathExpression, and XPathResult (some of which are used in this article's evaluateXPath() function), maps the DOM Core specification and the XPath specification , but not DOM HTML and XPath. Indeed, if you try to use any of the methods in the DOM XPath interfaces (e.g., XPathEvaluator.evaluate()) with DOM HTML interfaces, you will get an exception. In other words, XPath does not work with DOM HTML trees> Try it if you don't believe me. grimholtz
"Mozilla implements much of the DOM 3 XPath. This allows XPath expressions to be run against both HTML and XML documents." (Source: Mozilla XPath Documentation) Note that the examples running on that page work quite well, and that the page is not well-formed XML. --Unarmed 06:12, 30 Mar 2005 (PST)

The code on that page does not use the interfaces defined by the W3C for evaluation XPath expressions against a DOM Core object (Document Object Model XPath). It uses document.evaluate(), where document is an instance of HTMLDocument (not XMLDocument). I've never seen this method until now, and there doesn't appear to be any reference to Document.evaluate() in any of the W3C specifications I'm perusing. IE doesn't recognize it, nor do I see support for it in Xalan. Do you happen to know anything else about it? FWIW, the code in this article from before 16:58, 18 Mar 2005 does not work in Firefox with HTMLDocument DOMs. Save this as HTML and open in FireFox:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
// 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.documentElement);
  var result = xpe.evaluate(aExpr, aNode, nsResolver, 0, null);
  var found = [];
  while (res = result.iterateNext())
  return found;
<input type="edit"> <!-- note non-well-formed -->
<body onload="alert(evaluateXPath(document, '//body'));">

It yields a JavaScript error: aNode.ownerDocument has no properties. However, I made a simple change to the article on 16:58, 18 Mar 2005 so the code works with both XMLDocuments and HTMLDocuments. It's in the the second line of evaluateXPath():
var nsResolver = xpe.createNSResolver(aNode.ownerDocument == null ?
  aNode.documentElement : aNode.ownerDocument.documentElement);
This article uses the W3C standard DOM XPath interfaces and methods, not Document.evaluate() like the link you sent.
Right - disclaimer: I wrote the article being referenced. It's not finished or complete (corrections or additions are welcome) and is based more on what works in Mozilla than a detailed reading of the spec. Despite that, the DOM3 XPath Spec is quite clearly a DOM spec and appears to be designed to work with all DOM trees. In particular, sentences such as "In a DOM implementation which supports the XPath 3.0 feature, as described above, the XPathEvaluator interface will be implemented on the same object which implements the Document interface" suggest that document.evaluate is legitimate.
Regardless of what the spec intends, there is an implementation of XPath in Mozilla that works with HTML and XML (almost) interchangably. It is immensley useful; I've noticed many of the existing Greasemonkey user scripts use XPath to access particular DOM elements they want to manipulate and they're probably *all* working with HTML not XML. Since the purpose of the kb is to document Mozilla (and not the w3c specs) XPath in HTML is well worth documenting and document.evaluate provides (IMHO) the most transparent interface for that. Jgraham

You should feel free to edit this article as you see fit since it is in the public domain. However, there's something to be said for thoroughness, in addition to the value of showing off Mozilla's capabilities in that they've actually implemented all of these interfaces, so I would just ask that if you document document.evaluate(), please don't remove the existing code. Rather, you could highlight document.evaluate() as an alternative (and certainly easier and perhaps more performant) means to evaluate XPath expressions. --grimholtz