W3cubDocs

/Web APIs

Document: createExpression() method

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.

Syntax

js

createExpression(xpathText, namespaceURLMapper)

Parameters

xpathText

A string which is the XPath expression to be compiled.

namespaceURLMapper

A function which maps a namespace prefix to a namespace URL (or null if none needed).

Return value

Examples

js

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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
createExpression 1 12 1 No ≤12.1 3 3 18 4 ≤12.1 1 1.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Document/createExpression