document.createNSResolver() is a deprecated instance method related to XPath namespace resolution (see MDN Document: createNSResolver()). Learn what it used to do, why MDN says it now returns the input unchanged, how modern resolver functions replace it, and five try-it labs.
01
Kind
Instance method
02
Arg
Node
03
Returns
Same node
04
Was for
XPathNSResolver
05
Prefer
resolver fn
06
Status
Deprecated
Fundamentals
Introduction
XPath queries sometimes use prefixes like svg:circle. The engine needs a namespace resolver that maps svg to http://www.w3.org/2000/svg.
Historically, document.createNSResolver(node) built an XPathNSResolver from a node’s namespace context. MDN now documents a simpler reality: the method returns that node unchanged and is kept only so old scripts do not break.
💡
Learn it, don’t ship it
Recognize createNSResolver in legacy XPath samples. For new code, pass a (prefix) => namespaceURI function to evaluate / createExpression, or use null.
MDN: the method returns nodeResolver itself. There is no separate wrapper object in modern engines.
Example 2 — Legacy pattern you may see in old code
Recognize this shape — then migrate away from it.
JavaScript
// Legacy-looking sample (do not copy into new apps):
const contextNode = document.documentElement;
const resolver = document.createNSResolver(contextNode);
const result = document.evaluate(
"//*",
document,
resolver, // today this is just contextNode again
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
);
console.log(result.snapshotLength);
console.log(resolver === contextNode); // true
createExpression() accepts the same kind of mapper as evaluate, and reuses the compiled path.
Example 5 — No prefixes? Pass null
When XPath has no namespace prefixes, skip resolvers entirely.
JavaScript
const result = document.evaluate(
"count(//button)",
document,
null, // no namespace resolver needed
XPathResult.NUMBER_TYPE,
null
);
console.log(result.numberValue);
// Avoid createNSResolver for this case too
Document.createNSResolver() is Deprecated on MDN. Logos use the shared browser-image-sprite.png sprite from this project. Engines may still expose it as an identity helper for compatibility.
✓ Deprecated · Compatibility only
Document.createNSResolver()
Legacy XPathNSResolver helper — MDN says it now returns the input as-is.
LegacyCompatibility only
Google ChromeSupported (deprecated)
Legacy
Mozilla FirefoxSupported (deprecated)
Legacy
Apple SafariSupported (deprecated)
Legacy
Microsoft EdgeSupported (deprecated)
Legacy
OperaSupported (deprecated)
Legacy
Internet ExplorerLegacy support
Legacy
createNSResolver()Avoid in new code
Bottom line: Recognize createNSResolver in old XPath samples. For new queries, pass a resolver function or null to evaluate / createExpression.
Wrap Up
Conclusion
document.createNSResolver(node) is a deprecated compatibility method. MDN documents that it returns the input as-is. Learn it to read legacy XPath code; write new resolvers as plain functions (or use null when prefixes are absent).
MDN: Document.createNSResolver() used to create a custom XPathNSResolver. It now returns the input node as-is and is kept only for compatibility.
Yes. MDN marks Document.createNSResolver() as Deprecated. Avoid it in new code.
MDN: it returns nodeResolver itself — the same Node you passed in.
Pass a namespace resolver function (prefix => namespaceURI) directly to document.evaluate() or document.createExpression(), or use null when prefixes are not needed.
Older XPath APIs expected an XPathNSResolver object. createNSResolver(node) historically wrapped a node so prefix lookups could use that node's namespace context.
No for new code. Prefer an explicit resolver function or null. Recognize createNSResolver only when reading legacy samples.
Did you know?
Namespace URIs like http://www.w3.org/2000/svg are identifiers, not download links. Resolvers exist so the XPath engine can match the short prefix in your query to that identifier — the same idea as xmlns:svg="..." in XML markup.