Python Forum

Full Version: Reusing same namespace name when writing back XML with xml.etree.ElementTree
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm editing an XML file with xml.etree.ElementTree. On input the XML has a namespace defined so all tags are of the form
<somenamespace:tag>
When I write the XML back to file, the namespace is called ns0 and the tags look like:

<ns0:tag>
This is valid XML but not as readable. How can i tell Python to use "somenamespace" instead of "ns0"? I tried ET.register_namespace(prefix,namespace) but that doesn't work.
Oops, actually a problem in my code. ET.register_namespace(prefix,namespace) works provided that:

  1. It executed before reading the XML
  2. The namespace string is the correct one.