JavaScript Document createNSResolver() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.createNSResolver(nodeResolver) is a deprecated Document method from the XPath evaluator surface. Historically it built an XPathNSResolver from a node. Today it simply returns that same node so old scripts keep working.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call document.createNSResolver. Start with this line.

JavaScript
document.createNSResolver(nodeResolver)
CallWhat it does
document.createNSResolver(nodeResolver)Primary signature.
document.createNSResolver(...)Call with the arguments your case needs.

Built into the browser. Call createNSResolver(...) on document — no import required.

Library declarations
JavaScript
document.createNSResolver(nodeResolver)

Parameters

ParameterDescription
nodeResolverRequired. A Node (returned as-is today)

Return Value

Return ValueDescription
*Same node reference

JavaScript Examples

Example 1 — Returns the Same Node (MDN)

Today the method is an identity helper for the argument you pass.

JavaScript
const node = document.body;
const resolved = document.createNSResolver(node);

console.log(resolved === node);                // true
console.log(resolved.nodeName);                // "BODY"
console.log(typeof document.createNSResolver); // "function"
Try It Yourself

How It Works

  1. Pass a node. MDN parameter: nodeResolver.
  2. Call the method. Engines keep it for compatibility.
  3. Compare with ===. MDN: return value is the input itself.
  4. No wrapper. Do not expect a separate XPathNSResolver object today.

Example 2 — Legacy Pattern You May See

Recognize this shape in old tutorials — 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
Try It Yourself

How It Works

  1. Old tutorials called createNSResolver before evaluate.
  2. Third argument still accepts a resolver (node, function, or null).
  3. Identity today means the call adds no real mapping logic.
  4. Migrate to an explicit function or null when you edit the file.

Example 3 — Modern Resolver Function

Map svg: explicitly — no createNSResolver.

JavaScript
const svgNS = "http://www.w3.org/2000/svg";

function nsResolver(prefix) {
  const map = { svg: svgNS };
  return map[prefix] || null;
}

const result = document.evaluate(
  "count(//svg:circle)",
  document,
  nsResolver,
  XPathResult.NUMBER_TYPE,
  null
);

console.log(result.numberValue);
Try It Yourself

How It Works

  1. Store the URI in a named constant (SVG namespace string).
  2. Write a mapper that returns a URI for known prefixes, else null.
  3. Pass the function as the third argument to evaluate.
  4. XPath engine calls it for each prefix in the expression.

Browser Support

Document.createNSResolver() is Deprecated on MDN. Engines may still expose it as an identity helper for compatibility. Prefer resolver functions with evaluate / createExpression.

✓ Deprecated

Document.createNSResolver()

Legacy XPathNSResolver helper — MDN says it now returns the input as-is.

Legacy Compatibility only
Google Chrome Supported (deprecated)
Legacy
Microsoft Edge Supported (deprecated)
Legacy
Opera Supported (deprecated)
Legacy
Apple Safari Supported (deprecated)
Legacy
Mozilla Firefox Supported (deprecated)
Legacy
Internet Explorer Legacy support
Legacy
createNSResolver() Avoid in new code

Bottom line: Recognize createNSResolver in old XPath samples. For new queries, pass a resolver function or null. See the MDN createNSResolver() reference.

Frequently Asked Questions

MDN: it 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 projects.
MDN: it returns nodeResolver itself — the same Node you passed in.
Pass a namespace resolver function (prefix => namespaceURI or null) to document.evaluate() or document.createExpression(), or pass null when your XPath has no prefixes.
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.

Next: createProcessingInstruction()

Learn how to create XML processing instruction nodes such as xml-stylesheet.

createProcessingInstruction() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful