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.
xml
namespace prefix, within the XPath expression into appropriate namespace URIs.
A
XPathExpression
representing the compiled form of the XPath expression.
If the expression is not legal according to the rules of the
XPathEvaluator
,
XPathException
类型
INVALID_EXPRESSION_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()
方法。
<div>XPath example</div> <div>Number of <div>s: <output></output></div>
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. |
推荐 | 初始定义 |
No compatibility data found. Please contribute data for "api.XPathEvaluator.createExpression" (depth: 1) to the MDN 兼容性数据存储库 .
XPathEvaluator
createExpression()
createNSResolver()
evaluate()