Python Forum
Reusing same namespace name when writing back XML with xml.etree.ElementTree - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Reusing same namespace name when writing back XML with xml.etree.ElementTree (/thread-580.html)



Reusing same namespace name when writing back XML with xml.etree.ElementTree - Ofnuts - Oct-20-2016

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.


RE: Reusing same namespace name when writing back XML with xml.etree.ElementTree - Ofnuts - Oct-21-2016

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.