--- title: XPathEvaluator slug: Web/API/XPathEvaluator tags: - API - DOM - DOM XPath API - Document - Interface - NeedsTranslation - Reference - TopicStub - XML - XPath - XPathEvaluator translation_of: Web/API/XPathEvaluator ---
{{APIRef("DOM XPath")}}
XPathEvaluator
接口能够对 {{Glossary("XPath")}} 表达式进行编译和求值。
该接口实现自{{domxref("Document")}}的接口。
下面的实例展示了如何使用XPathEvaluator
接口。
<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;
{{EmbedLiveSample('Example', 400, 70)}}
Specification | Status | Comment |
---|---|---|
{{SpecName("DOM3 XPath", "xpath.html#XPathEvaluator", "XPathEvaluator")}} | {{Spec2("DOM3 XPath")}} | Initial definition |
{{Compat("api.XPathEvaluator")}}