This method compiles an XPathExpression which can then be used for (repeated) evaluations of the XPath 表达式。

句法

XPathExpression XPathEvaluator.createExpression(expression, resolver);
					

参数

表达式
DOMString representing representing the XPath expression to be created.
resolver 可选
Permits translation of all prefixes, including the xml namespace prefix, within the XPath expression into appropriate namespace URIs.

返回值

A XPathExpression representing the compiled form of the XPath expression.

异常

INVALID_EXPRESSION_ERR

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

NAMESPACE_ERR

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

范例

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

HTML

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

JavaScript

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

结果

规范

规范 状态 注释
DOM (文档对象模型) 3 级 XPath 规范
The definition of 'XPathEvaluator.createExpression()' 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.createExpression" (depth: 1) to the MDN 兼容性数据存储库 .

另请参阅

元数据

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