files/en-us/web/api/document/createexpression/index.md
{{APIRef("DOM")}}
This method compiles an {{DOMxRef("XPathExpression")}} which can then be used for (repeated) evaluations.
You must call this method on the same document that you run the expression against.
createExpression(xpathText, namespaceURLMapper)
xpathText
namespaceURLMapper
{{DOMxRef("XPathExpression")}}
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
{{Specifications}}
{{Compat}}