Stripping namespaces from an XML document can simplify processing and parsing when namespaces are not required for your application logic. This can be particularly useful for data transformation or integration tasks. Here’s a step-by-step guide on how to remove namespaces from an XML document using different programming approaches.
One common method to strip namespaces is using XSLT (Extensible Stylesheet Language Transformations). XSLT can transform XML documents by removing unnecessary namespace declarations. Below is an example of a simple XSLT stylesheet that strips namespaces:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*"> <xsl:element name="{local-name()}"> <xsl:apply-templates select="@* | node()"/> </xsl:element> </xsl:template> <xsl:template match="@* | text()"> <xsl:copy/> </xsl:template> </xsl:stylesheet>
To apply this XSLT transformation, you can use an XSLT processor available in many programming environments, such as Python with the lxml library:
from lxml import etree xml_input = '''<root xmlns:ns="http://example.com"> <ns:child>Content</ns:child> </root>''' xslt = etree.XML('''<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*"> <xsl:element name="{local-name()}"> <xsl:apply-templates select="@* | node()"/> </xsl:element> </xsl:template> <xsl:template match="@* | text()"> <xsl:copy/> </xsl:template> </xsl:stylesheet>''') transform = etree.XSLT(xslt) doc = etree.XML(xml_input) result = transform(doc) print(etree.tostring(result, pretty_print=True).decode())
For those working in Java, you can use the javax.xml.transform package to perform a similar transformation. Here’s an example:
import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.dom.*; import org.w3c.dom.*; public class StripNamespaces { public static void main(String[] args) throws Exception { String xmlInput = "<root xmlns:ns='http://example.com'><ns:child>Content</ns:child></root>"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xmlInput))); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Source xslt = new StreamSource(new StringReader( "<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" + "<xsl:template match='*'>" + "<xsl:element name='{local-name()}'>" + "<xsl:apply-templates select='@* | node()'/>" + "</xsl:element>" + "</xsl:template>" + "<xsl:template match='@* | text()'>" + "<xsl:copy/>" + "</xsl:template>" + "</xsl:stylesheet>" )); Transformer transformer = transformerFactory.newTransformer(xslt); transformer.transform(new DOMSource(document), new StreamResult(System.out)); } }
By using these methods, you can effectively strip namespaces from an XML document, making it easier to handle in applications where namespaces are not needed. This can simplify XML processing and help avoid potential issues related to namespace conflicts.







