Assume that I\'ve the following XML which I want to modify using Python\'s ElementTree
:
Here is the way to preserve the namespaces' prefix and URI:
def register_all_namespaces(filename):
namespaces = dict([node for _, node in ET.iterparse(filename, events=['start-ns'])])
for ns in namespaces:
ET.register_namespace(ns, namespaces[ns])
This method should be called before calling the [ET].write()
method.