evaluate() 方法在 XPathEvaluator interface executes an XPath expression on the given node or document and returns an XPathResult .

句法

XPathResult XPathEvaluator.evaluate(expression, contextNode, resolver, type, result);
					

参数

表达式
DOMString representing the XPath expression to be parsed and evaluated.
contextNode
节点 representing the context to use for evaluating the expression.
resolver 可选
Permits translation of all prefixes, including the xml namespace prefix, within the XPath expression into appropriate namespace URIs.
type 可选
Specifies the type of result to be returned by evaluating the expression. This must be one of the XPathResult.Constants .
result 可选
Allows to specify a result object which may be reused and returned by this method. If this is specified as null or the implementation does not reuse the specified result, a new result object will be returned.

返回值

XPathResult object representing the result of evaluating the XPath expression.

异常

INVALID_EXPRESSION_ERR

If the expression is not legal according to the rules of the XPathEvaluator XPathException 类型 INVALID_EXPRESSION_ERR 被引发。

TYPE_ERR

In case result cannot be converted to the specified type, an XPathException 类型 TYPE_ERR 被引发。

NAMESPACE_ERR

If the expression contains namespace prefixes which cannot be resolved by the specified XPathNSResolver DOMException 类型 NAMESPACE_ERROR 被引发。

WRONG_DOCUMENT_ERR

If the provided context node is from a document that is not supported by the XPathEvaluator DOMException 类型 WRONG_DOCUMENT_ERR 被引发。

NOT_SUPPORTED_ERR

If the provided context node is not a type permitted as an XPath context node or the request type is not permitted by the XPathEvaluator DOMException 类型 NOT_SUPPORTED_ERR 被引发。

范例

The following example shows the use of the evaluate() 方法。

HTML

<div>XPath example</div>
<div>Number of <div>s: <output></output></div>
					

JavaScript

var evaluator = new XPathEvaluator();
var result = evaluator.evaluate("//div", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
document.querySelector("output").textContent = result.snapshotLength;
					

结果

规范

规范 状态 注释
DOM (文档对象模型) 3 级 XPath 规范
The definition of 'XPathEvaluator.evaluate()' in that specification.
推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

No compatibility data found. Please contribute data for "api.XPathEvaluator.evaluate()" (depth: 1) to the MDN 兼容性数据存储库 .

元数据

  • 最后修改:
  1. XPathEvaluator
  2. 方法
    1. createExpression()
    2. createNSResolver()
    3. evaluate()
  3. 实现通过:
    1. Document