document.createExpression() is an instance method that compiles an XPath string into an XPathExpression you can evaluate again and again (see MDN Document: createExpression()). Learn how it differs from a one-shot document.evaluate(), how to reuse expressions on different contexts, optional namespace mapping, and five try-it labs.
01
Kind
Instance method
02
Input
XPath string
03
Returns
XPathExpression
04
Next
.evaluate()
05
Best for
Reuse
06
Status
Baseline
Fundamentals
Introduction
XPath is a query language for selecting nodes in an XML/HTML tree — similar in spirit to CSS selectors, but with a different syntax and more power for some document shapes.
document.createExpression(xpathText) does not run the query yet. It compiles the path into an XPathExpression. Then you call xpathExpr.evaluate(contextNode) to get an XPathResult. MDN highlights that the compiled expression can be reused for repeated evaluations.
💡
Compile once, evaluate many times
MDN sample: create //div once, evaluate against document, then again against a nav context node.
MDN also requires: call createExpression on the same document you will evaluate against.
An instance method on the page’s document object (part of the XPathEvaluatorBase API on Document; MDN).
xpathText — string containing the XPath expression to compile (MDN).
namespaceURLMapper — optional function mapping a prefix to a namespace URL, or null (MDN).
Return value — a compiled XPathExpression.
Evaluate — xpathExpr.evaluate(context) returns an XPathResult (MDN).
Same document — create and run against the same document (MDN).
Reuse — keep the expression object and evaluate it on different contexts.
Foundation
📝 Syntax
General form of Document.createExpression (MDN):
JavaScript
createExpression(xpathText, namespaceURLMapper)
Parameters
xpathText — string; the XPath expression to compile (MDN).
namespaceURLMapper — function that maps a namespace prefix to a namespace URL, or null if none is needed (MDN). Typical signature: (prefix) => namespaceURI | null.
Document.createExpression() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.createExpression()
Compile reusable XPathExpression objects across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
createExpression()Wide
Bottom line: Compile with createExpression when you reuse XPath. For simple HTML selection, prefer querySelector / querySelectorAll.
Wrap Up
Conclusion
document.createExpression(xpathText) compiles an XPath string into a reusable XPathExpression. Evaluate it against the document or a smaller context node whenever you need the same query more than once — exactly the pattern MDN demonstrates.
Compile once when evaluating the same path repeatedly
Create the expression on the same document you query (MDN)
Pass a context node to narrow the search
Choose an explicit XPathResult type for clear reads
Use a namespace mapper when XPath prefixes are required
❌ Don’t
Expect createExpression alone to return matching nodes
Cross documents with a compiled expression from elsewhere (MDN)
Reach for XPath when a simple CSS selector is enough
Forget null / a mapper when prefixes appear in the path
Mutate the DOM mid-iteration without knowing iterator invalidation rules
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createExpression()
Compile XPath once; evaluate it many times.
5
Core concepts
📝01
Returns
XPathExpression
MDN
🔄02
Reuse
evaluate again
key idea
📄03
Same doc
required
MDN
⚡04
Alt
document.evaluate
one-shot
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.createExpression() compiles an XPathExpression from an XPath string. You can then call evaluate() on that compiled expression — including repeatedly against different contexts.
No. MDN marks Document.createExpression() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
An XPathExpression object. Call xpathExpr.evaluate(contextNode) to get an XPathResult (MDN example).
Yes. MDN: you must call createExpression on the same document that you run the expression against.
Use createExpression when you will run the same XPath many times (MDN highlights reuse). For a one-off query, document.evaluate(xpath, context, ...) is often enough.
MDN: an optional function that maps a namespace prefix to a namespace URL, or null if none is needed. Use it when your XPath uses prefixes for XML/SVG namespaces.
Did you know?
XPath and CSS selectors both ask “which nodes?”, but XPath can return numbers, strings, and booleans too — for example count(//button). That is why XPathResult has numberValue and friends, not only node iterators.