XPath
This method compiles an XPathExpression which can then be used for (repeated) evaluations.
You must call this method on the same document that you run the expression against.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 1 | 12 | 1 | 3 | 18 | 1 | |
| This method compiles an XPathExpression which can then be used for (repeated) evaluations. | 1 | 12 | 1 | 3 | 18 | 1 |
| The evaluate() method of the Document interface selects elements based on the XPath expression given in parameters. | 1 | 12 | 1 | 3 | 18 | 1 |
| This method compiles an XPathExpression which can then be used for (repeated) evaluations of the XPath expression. | 1 | 12 | 1 | 3 | 18 | 1 |
| The evaluate() method of the XPathEvaluator interface executes an XPath expression on the given node or document and returns an XPathResult. | 1 | 12 | 1 | 3 | 18 | 1 |
| The XPathEvaluator() constructor creates a new XPathEvaluator. | 1 | 12 | 1 | 3 | 18 | 1 |
| This interface is a compiled XPath expression that can be evaluated on a document or specific node to return information from its DOM tree. | 1 | 12 | 1 | 3 | 18 | 1 |
| The evaluate() method of the XPathExpression interface executes an XPath expression on the given node or document and returns an XPathResult. | 1 | 12 | 1 | 3 | 18 | 1 |
| The XPathResult interface represents the results generated by evaluating an XPath expression within the context of a given node. | 1 | 12 | 1 | 3 | 18 | 1 |
| The read-only booleanValue property of the XPathResult interface returns the boolean value of a result with XPathResult.resultType being BOOLEAN_TYPE. | 1 | 12 | 1 | 3 | 18 | 1 |
| The read-only invalidIteratorState property of the XPathResult interface signifies that the iterator has become invalid. It is true if XPathResult.resultType is UNORDEREDNODEITERATORTYPE or ORDEREDNODEITERATORTYPE and the document has been modified since this result was returned. | 1 | 12 | 1 | 3 | 18 | 1 |
| The iterateNext() method of the XPathResult interface iterates over a node set result and returns the next node from it or null if there are no more nodes. | 1 | 12 | 1 | 3 | 18 | 1 |
| The read-only numberValue property of the XPathResult interface returns the numeric value of a result with XPathResult.resultType being NUMBER_TYPE. | 1 | 12 | 1 | 3 | 18 | 1 |
| The read-only resultType property of the XPathResult interface represents the type of the result, as defined by the type constants. | 1 | 12 | 1 | 3 | 18 | 1 |
| The read-only singleNodeValue property of the XPathResult interface returns a Node value or null in case no node was matched of a result with XPathResult.resultType being ANYUNORDEREDNODETYPE or FIRSTORDEREDNODETYPE. | 1 | 12 | 1 | 3 | 18 | 1 |
| The snapshotItem() method of the XPathResult interface returns an item of the snapshot collection or null in case the index is not within the range of nodes. Unlike the iterator result, the snapshot does not become invalid, but may not correspond to the current document if it is mutated. | 1 | 12 | 1 | 3 | 18 | 1 |
| The read-only snapshotLength property of the XPathResult interface represents the number of nodes in the result snapshot. | 1 | 12 | 1 | 3 | 18 | 1 |
| The read-only stringValue property of the XPathResult interface returns the string value of a result with XPathResult.resultType being STRING_TYPE. | 1 | 12 | 1 | 3 | 18 | 1 |
Syntax
const xpathExpr = document.createExpression("//div");
const xpathResult = xpathExpr.evaluate(document); // returns an XPathResult object
const nodeContext = document.querySelector("nav");
// Re-using the XPathExpression "xpathExpr"
const otherResult = xpathExpr.evaluate(nodeContext); // returns an XPathResult object Use cases
-
Using XPath
This method compiles an XPathExpression which can then be used for (repeated) evaluations.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.