Stripping Namespace

Working with XML data in JavaScript can become challenging when XML namespaces are involved.

These namespaces, while crucial for distinguishing XML elements from different vocabularies, often interfere with parsing and querying tasks, especially when using XPath or DOM manipulation.

This article walks you through multiple effective ways to stripping namespace or handle it using JavaScript, including modern techniques suited for browser and Node.js environments.

Overview Of The General Problem

Here’s a typical namespaced XML snippet:

<root xmlns="http://example.com/ns">
  <item>
    <name>Example</name>
  </item>
</root>

If you try querying //item/name using XPath or getElementsByTagName(‘item’) in JavaScript, it fails due to the default namespace being applied to all elements. This leads to the need for either:

  • Stripping the namespace altogether, or
  • Handling it properly using a resolver.

Ways To Stripping Namespace

Stripping Namespace

Solution 1: Remove xmlns Attributes and Namespace Prefixes with Regex

This is a quick way to strip namespaces when you don’t need them.

let xmlString = new XMLSerializer().serializeToString(xmlDoc);
xmlString = xmlString.replace(/xmlns(:\w+)?="[^"]*"/g, '');
xmlString = xmlString.replace(/(<\/?)[\w\d]+:/g, '$1');
let cleanXml = new DOMParser().parseFromString(xmlString, "application/xml");

This solution serializes the XML into a string and then removes any xmlns attributes that define namespaces using a regular expression.

It also strips any namespace prefixes from element tags (e.g., turning into ). The result is then parsed back into a DOM object.

This is fast and useful when you’re confident about the structure of the XML, but it can fail or cause errors if the regex doesn’t match more complex namespaces.

Relevant Posts You May Like

Solution 2: Use XPath with a Namespace Resolver

function nsResolver(prefix) {
  const ns = {
    ns: "http://example.com/ns"
  };
  return ns[prefix] || null;
}

let result = xmlDoc.evaluate(
  '/ns:root/ns:item/ns:name',
  xmlDoc,
  nsResolver,
  XPathResult.STRING_TYPE,
  null
).stringValue;

Instead of removing the namespace, this approach handles it properly by using a namespace resolver with XPath.

The nsResolver function maps a prefix (ns) to the namespace URI, allowing you to use that prefix in your XPath queries.

It’s a robust solution if you know the namespace URI in advance and want to keep the structure and semantics of the original XML.

Solution 3: Use localName to Access Elements Without Worrying About Namespaces

let elements = xmlDoc.getElementsByTagName("*");
for (let el of elements) {
  if (el.localName === "name") {
    console.log(el.textContent);
  }
}

This solution doesn’t modify the XML at all. Instead, it loops through every element and compares the localName — the part of the element name that ignores the namespace.

It’s a simple and readable solution that works well if you don’t want to deal with XPath and only need to find specific tag names regardless of their namespace.

Solution 4: Recursive Function to Remove Namespace Attributes from the DOM

function removeNSAttrs(node) {
  if (node.attributes) {
    for (let i = node.attributes.length - 1; i >= 0; i--) {
      if (node.attributes[i].name.startsWith("xmlns")) {
        node.removeAttribute(node.attributes[i].name);
      }
    }
  }

  for (let i = 0; i < node.childNodes.length; i++) {
    removeNSAttrs(node.childNodes[i]);
  }
}

removeNSAttrs(xmlDoc.documentElement);

This method recursively walks through every element and removes any xmlns attributes that define namespaces.

It preserves the XML tree structure while stripping namespace declarations, making it a safer alternative to regex.

It’s especially useful when you need a clean document without altering tag names or values.

Solution 5: Use XSLT to Transform and Remove Namespaces

let xslt = `
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="*">
      <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@* | node()"/>
      </xsl:element>
    </xsl:template>
    <xsl:template match="@*">
      <xsl:attribute name="{local-name()}">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:template>
    <xsl:template match="text() | comment() | processing-instruction()">
      <xsl:copy/>
    </xsl:template>
  </xsl:stylesheet>
`;

let xsltDoc = new DOMParser().parseFromString(xslt, "application/xml");
let processor = new XSLTProcessor();
processor.importStylesheet(xsltDoc);
let transformed = processor.transformToDocument(xmlDoc);

This solution uses XSLT to generate a new XML document by copying all elements and attributes using only their local names.

All namespace declarations and prefixes are omitted in the output. This is a powerful and standards-compliant approach that works well for transforming entire documents while preserving structure and content.

Relevant Posts You May Like

Solution 6: XPath with local-name() to Ignore Namespaces

let result = xmlDoc.evaluate(
  "//*[local-name()='name']",
  xmlDoc,
  null,
  XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
  null
);

for (let i = 0; i < result.snapshotLength; i++) {
  console.log(result.snapshotItem(i).textContent);
}

This solution uses the XPath local-name() function to select elements by their tag names only, ignoring any namespace they may belong to.

It allows for highly flexible XPath expressions without needing a namespace resolver. It’s perfect when you don’t know (or care about) the namespaces and just want to find certain tag names.

Solution 7: Custom Namespace-agnostic Search Function (Inspired by Community)

function findElementsByLocalName(doc, name) {
  let all = doc.getElementsByTagName("*");
  return Array.from(all).filter(el => el.localName === name);
}

let names = findElementsByLocalName(xmlDoc, "name");
names.forEach(el => console.log(el.textContent));

This wraps the earlier localName approach into a reusable function, allowing for easier searches across the XML document without dealing with namespaces.

It’s great for projects where namespace handling needs to be abstracted away or made more readable in your codebase.

Conclusion

Removing or bypassing XML namespaces in JavaScript is often necessary, and this article has shown every popular solution in use today:

  • Use regex when you need a quick string-based cleanup (but be careful with structure).
  • Use namespace resolvers in XPath for accurate querying with preserved structure.
  • Use localName or custom filters to sidestep namespace issues without altering the XML.
  • Use recursive namespace attribute removal if you want to keep structure but drop the declarations.
  • Use XSLT for professional-grade transformations that strip all namespaces cleanly.
  • Use XPath with local-name() to query any tag regardless of namespace.

Pick the right one based on whether you’re parsing, transforming, stripping namespace, or just extracting specific content from XML.

Help Someone By Sharing This Article