I am trying to modify XML file using xml.etree.ElementTree on Python 2.6.6 (due to restrictions) and facing ns0 issue. I looked at this issue and used ET._namespace_map[uri] = prefix as suggested which removed ns0 but the element tags still has the : value. How do we remove it or does it impact the validity of the XML file when we use if for further processing?
Example:
<?xml version="1.0" encoding="UTF-8" ?>
<Seed xmlns="http://www.example.com">
<TagA>
<TagB>B</TagB>
<TagC>c</TagC>
</TagA>
</Seed>
Script
import xml.etree.ElementTree as ET
tree = ET.parse('sample.xml')
root = tree.getroot()
try:
ET.register_namespace("","http://example.com")
except AttributeError:
def register_namespace(prefix, uri):
ET._namespace_map[uri] = prefix
register_namespace("","http://www.example.com")
tree.write('sample.xml')
Note: I could not use lxml or other xml.etree that is supported only from 2.7 version.