With lxml
, I am not sure how to properly remove the namespace of an existing element and set a new one.
For instance, I'm parsing this minimal xml file:
<myroot xmlns="http://myxml.com/somevalue">
<child1>blabla</child1>
<child2>blablabla</child2>
</myroot>
... and I'd like it to become:
<myroot xmlns="http://myxml.com/newvalue">
<child1>blabla/child1>
<child2>blablabla</child2>
</myroot>
With lxml
:
from lxml import etree as ET
tree = ET.parse('myfile.xml')
root= tree.getroot()
If I inspect root
:
In [7]: root
Out[7]: <Element {http://myxml.com/somevalue}myroot at 0x7f6e13832588>
In [8]: root.nsmap
Out[8]: {None: 'http://myxml.com/somevalue'}
In [11]: root.tag
Out[11]: '{http://myxml.com/somevalue}myroot'
Ideally, I would like to end up with:
In [8]: root.nsmap
Out[8]: {None: 'http://myxml.com/newvalue'}
In [11]: root.tag
Out[11]: '{http://myxml.com/newvalue}myroot'
As for the tag, it's just a matter of setting the right string. How about nsmap
?