document.evaluate

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

摘要

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

语法

var xpathResult = document.evaluate(
 xpathExpression, 
 contextNode, 
 namespaceResolver, 
 resultType, 
 result
);
  • xpathExpression 表示要计算的Xpath字符串。
  • contextNode 表示本次查询的上下文节点 (参照XPath规范 [http://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对象。

示例

var headings = document.evaluate("/html/body//h2", document, null, XPathResult.ANY_TYPE, null); 
/* 在document中查找所有的h2元素。  
 * 结果可能是无序节点迭代器。 */
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); // 显示所有h2节点的文本

注意,在上述例子中,最好写更冗长的XPath,而不是常用的简写,比如//h2。 通常,像上述例子所示,更具体的XPath选择器会得到显著的性能提升,特别是在非常大的文档中。这是因为查询计算不会将时间浪费在查看不需要的节点上。使用 // 通常很慢,这是因为它要从根节点和所有子节点中查找所有可能匹配的节点。

通过谨慎使用上下文参数能得到进一步的优化。比如,如果你知道你要查找的内容在body标签的某处,你可以这样做:

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

注意上面的document.body已经替代了document作为上下文,所以 XPath从body元素开始查找。 (在这个例子中,"."很重要,因为它指示了查找要从document.body这个上下文节点开始。如果遗漏了 "." (剩下//h2) ,查找会从根节点 (html) 开始,这样会很浪费。)

查阅 Introduction to using XPath in JavaScript 获得更多信息。

注意

  • XPath表达式能在HTML和XML文档上计算。
  • 如果要计算别的而不是当前文档,在FF3上必须使用someXMLDoc.evaluate() ,虽然在FF2上document.evaluate()也有效。

结果的类型

(Merge with Template:XPathResultConstants?

这些是evaluate 方法的resultType参数支持的值:

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.

浏览器兼容性

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 ? ? ? ? ?

规范

参考

文档标签和贡献者

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