I am stitching together XML files using the lxml.etree library and namespaces are being dropped on write.
Input.xml
<?xml version="1.0" encoding="UTF-8"?>
<haul>
<uuid>abc</uuid>
<port xmlns="hello"
xmlns:a="hello">
<v>
<a:value>0</a:value>
<type>int</type>
</v>
</port>
</haul>
I tried this code:
test.py
tree = load_tree_from_file('Input.xml')
port = tree.find('{hello}port')
v = port.find('{hello}v')
v2=copy.deepcopy(v)
port.append(v2)
d = os.path.dirname(__file__)
tree.write(os.path.join(d, 'Output.xml'))
I got this output:
Output.xml
<haul>
<uuid>abc</uuid>
<port xmlns="hello" xmlns:a="hello">
<v>
<a:value>0</a:value>
<type>int</type>
</v>
<v>
<value>0</value>
<type>int</type>
</v>
</port>
</haul>
I expected–and need–the <value>
Element to be <a:value>
like the first one.