I'm using ElementTree in Python to parse an xml file and add or remove elements in it.
In my XML file the root and the elements just below the root have a namespace, but all the other elements do not.
I see that ElementTree, when printing the modified tree, adds namespaces to every element.
Is there a proper way of telling ElementTree to just keep namespaces in the elements where they originally appeared?
Try with this:
import xml.etree.ElementTree as ET
namespaces = {
'': 'http://tempuri.org/',
'soap': 'http://schemas.xmlsoap.org/soap/envelope/',
'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xsd': 'http://www.w3.org/2001/XMLSchema',
}
for prefix, uri in namespaces.items():
ET.register_namespace(prefix, uri)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With