Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

document.evaluate

这篇翻译不完整。请帮忙从英语翻译这篇文章

摘要

根据传入的 XPath 表达式以及其他参数,返回一个 XPathResult 对象。

语法

var xpathResult = document.evaluate(
 xpathExpression, 
 contextNode, 
 namespaceResolver, 
 resultType, 
 result
);
  • xpathExpression 是表示Xpath的字符串。
  • contextNode 表示本次查询的 context node (参照XPath规范 [https://www.w3.org/TR/xpath])。通常会使用document
  • namespaceResolver 是函数。传入名空间前缀,返回跟此前缀相关的名空间URI(字符串)。通常用来解析Xpath内的前缀,以便对文档进行匹配。HTML文档或者不使用名空间前缀的文档,通常传入null
  • resultType 是整数。指定所返回的 XPathResult 的类型,常常使用 named constant properties,如 XPathResult.ANY_TYPE,范围 0 到 9,见下表。
  • result 为XPathResult型,用以存储查询结果。通常传入null,此时将创建新的XPathResult对象。

Example

var headings = document.evaluate("/html/body//h2", document, null, XPathResult.ANY_TYPE, null); 
/* Search the document for all h2 elements.  
 * The result will likely be an unordered node iterator. */
var thisHeading = headings.iterateNext(); 
var alertText = "Level 2 headings in this document are:\n";
while (thisHeading) {
  alertText += thisHeading.textContent + "\n";
  thisHeading = headings.iterateNext();
}
alert(alertText); // Alerts the text of all h2 elements

Note, in the above example, a more verbose XPath is preferred over common shortcuts such as //h2. Generally, more specific XPath selectors as in the above example usually gives a significant performance improvement, especially on very large documents. This is because the evaluation of the query spends does not waste time visiting unnecessary nodes. Using // is generally slow as it visits every node from the root and all subnodes looking for possible matches.

Further optimization can be achieved by careful use of the context parameter. For example, if you know the content you are looking for is somewhere inside the body tag, you can use this:

document.evaluate(".//h2", document.body, null, XPathResult.ANY_TYPE, null);

Notice in the above document.body has been used as the context instead of document so the XPath starts from the body element. (In this example, the "." is important to indicate that the querying should start from the context node, document.body. If the "." was left out (leaving //h2) the query would start from the root node (html) which would be more wasteful.)

See Introduction to using XPath in JavaScript for more information.

Notes

  • XPath expressions can be evaluated on HTML and XML documents.
  • While using document.evaluate() works in FF2, in FF3 one must use someXMLDoc.evaluate() if evaluating against something other than the current document.

Result types

(Merge with Template:XPathResultConstants?

These are supported values for the resultType parameter of the evaluate method:

Result Type Value Description
ANY_TYPE 0 Whatever type naturally results from the given expression.
NUMBER_TYPE 1 A result set containing a single number. Useful, for example, in an XPath expression using the count() function.
STRING_TYPE 2 A result set containing a single string.
BOOLEAN_TYPE 3 A result set containing a single boolean value. Useful, for example, an an XPath expression using the not() function.
UNORDERED_NODE_ITERATOR_TYPE 4 A result set containing all the nodes matching the expression. The nodes in the result set are not necessarily in the same order they appear in the document.
ORDERED_NODE_ITERATOR_TYPE 5 A result set containing all the nodes matching the expression. The nodes in the result set are in the same order they appear in the document.
UNORDERED_NODE_SNAPSHOT_TYPE 6 A result set containing snapshots of all the nodes matching the expression. The nodes in the result set are not necessarily in the same order they appear in the document.
ORDERED_NODE_SNAPSHOT_TYPE 7 A result set containing snapshots of all the nodes matching the expression. The nodes in the result set are in the same order they appear in the document.
ANY_UNORDERED_NODE_TYPE 8 A result set containing any single node that matches the expression. The node is not necessarily the first node in the document that matches the expression.
FIRST_ORDERED_NODE_TYPE 9 A result set containing the first node in the document that matches the expression.

Results of NODE_ITERATOR types contain references to nodes in the document. Modifying a node will invalidate the iterator. After modifying a node, attempting to iterate through the results will result in an error.

Results of NODE_SNAPSHOT types are snapshots, which are essentially lists of matched nodes. You can make changes to the document by altering snapshot nodes. Modifying the document doesn't invalidate the snapshot; however, if the document is changed, the snapshot may not correspond to the current state of the document, since nodes may have moved, been changed, added, or removed.

Browser compatibility

Basic test case, if you want to note coverage for your browser.

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit) Konqueror (KTHLM)
XPath 1.0 support 1 1.5 (1.8) - (not in IE10) 9.00 5.0 (531) or earlier 4.8 or earlier (not in 4.4)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
XPath 1.0 ? ? ? ? ?

Specification

See also

文档标签和贡献者

 此页面的贡献者: teoli, lyklykkkkkkk
 最后编辑者: lyklykkkkkkk,