I have an XML file in the following format
<?xml version="1.0" encoding="utf-8"?>
<foo>
<bar>
<bat>1</bat>
</bar>
<a>
<b xmlns="urn:schemas-microsoft-com:asm.v1">
<c>1</c>
</b>
</a>
</foo>
I want to change the value of bat to '2' and change the file to this:
<?xml version="1.0" encoding="utf-8"?>
<foo>
<bar>
<bat>2</bat>
</bar>
<a>
<b xmlns="urn:schemas-microsoft-com:asm.v1">
<c>1</c>
</b>
</a>
</foo>
I open this file by doing this
tree = ET.parse(filePath)
root = tree.getroot()
I then change the value of bat to '2' and save the file like this:
tree.write(filePath, "utf-8", True, None, "xml")
The value of bat successfully changes to 2, but the XML file now looks like this.
<?xml version="1.0" encoding="utf-8"?>
<foo xmlns:ns0="urn:schemas-microsoft-com:asm.v1">
<bar>
<bat>2</bat>
</bar>
<a>
<ns0:b>
<ns0:c>1</ns0:c>
</ns0:b>
</a>
</foo>
In order to fix the issue of having a namespace named ns0, I do the following before parsing the document
ET.register_namespace('', "urn:schemas-microsoft-com:asm.v1")
This gets rid of the ns0 namepace but the xml file now looks like this
<?xml version="1.0" encoding="utf-8"?>
<foo xmlns="urn:schemas-microsoft-com:asm.v1">
<bar>
<bat>2</bat>
</bar>
<a>
<b>
<c>1</c>
</b>
</a>
</foo>
What do I do to get the output I need?