Skip to content
Remove elements of a certain namespace from XML data
Python JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



                                remove namespace

These files provide possibilities to remove elements and attributes from an XML
document, that are in a certain, defined namespace.

Consider you have an XHTML document with math formulas in the MathML namespace.
For some reason, you want the formulas stripped out of the file. Nothing
simpler than that! Use the tools provided by this project and enjoy.

The package contains three different implementations, that all work the same
way. You can use any of the three XSLT, native Javascript or native Python
implementation independently.

P a r a m e t e r s :

* namespace: The namespace whose elements and attributes are to be removed.

* copy_children: If true, the children of matching elements will be copied.
  Otherwise, they will be erased, too. Default is 'true'.

* remove_attributes: This parameter controls, if attributes are removed, too.
  Attributes are per definitionem in the empty namespace, as long as they are
  not explicitly, that is, with a prefix, bound to a namespace. That means,
  that if elements in the empty namespace should be removed, most of the
  attributes of other elements will vanish as well. This parameter allows to
  address this problem. Default is 'true'.

B e h a v i o u r :

* If the root element is bound to the namespace, that should be removed, the
  result could be invalid XML. Therefore in the XSLT version in this case the
  whole document is embedded in a new root element <root /> in the empty
  namespace and a warning message is issued.

H o w   t o   D e p l o y :

XSLT version:
  a) in Firefox: Add the following lines to your XML file:

     <?xslt-param name="namespace" value="urn:my-unwanted-namespace"?>
     <?xml-stylesheet type="text/xsl" href="rm-ns.xsl"?>

     (other browsers don't support <?xslt-param ?>, you have to touch
     rm-ns.xsl itself there.)

  b) via a command line XSLT processor:

     $ saxon -s:source.xml -xsl:rm-ns.xsl -o:out.xml \

     $ xalan -IN source.xml -XSL rm-ns.xsl -OUT out.xml -PARAM namespace \

  c) inside PHP:

     $xsl = new DOMDocument;
     $proc = new XSLTProcessor;

     $xml = new DOMDocument;
     $proc->setParameter('', 'namespace', 'urn:my-unwanted-namespace');
     $proc->transformToURI($xml, 'file:///tmp/output.xml');

  d) in Python with libxml2 and libxslt bindings:

     #! /usr/bin/env python

     import libxml2, libxslt

     styledoc = libxml2.parseFile("rm-ns.xsl")
     style = libxslt.parseStylesheetDoc(styledoc)
     doc = libxml2.parseFile('source.xml')
     result = style.applyStylesheet(doc, {"namespace":

     out = open('output.xml', 'w')


JS version:
  Add the following lines to your XML file:

  <script type="text/javascript" src="rm-ns.js"></script>
  <script type="text/javascript">remove_namespace("urn:my-unwanted-namespace");</script>

  Presto! Namespace removed.

Native Python version:

  >>> from rm_ns import remove_namespace
  >>> from xml.dom.minidom import parse
  >>> source = parse('source.xml')
  >>> remove_namespace("urn:my-unwanted-namespace", source)
  >>> out = open('output.xml', 'w')
  >>> out.write(source.toxml())
  >>> out.close()

L i c e n s e :

The tools are published under an MIT-style license and the GPL v2. Choose at
your liking.

You can’t perform that action at this time.