dom / latest / document / createexpression.html /

Document.createExpression()

This method compiles an XPathExpression which can then be used for (repeated) evaluations.

Syntax

createExpression(xpathText, namespaceURLMapper)

Parameters

  • xpathText is a string which is the XPath expression to be compiled.
  • namespaceURLMapper is a function which maps a namespace prefix to a namespace URL (or null if none needed).

Firefox 3 note: Prior to Firefox 3, you could call this method on documents other than the one you planned to run the XPath against. Under Firefox 3, you must call it on the same document.

Return value

Examples

let xpathExpr = document.createExpression('//div');
let xpathResult = xpathExpr.evaluate(document); // returns an XPathResult object
let nodeContext = document.getElementsByTagName('nav')[0];
// Re-using the XPathExpression "xpathExpr"
let 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–2021 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