I have a problem with namespacing in lxml 2.3 and etree.
For example, I have two nodes with different namespaces:
parent = etree.Element('{parent-space}parent')
child = etree.Element('{child-space}child')
After that, child node is appended to parent node:
parent.append(child)
Then, if I use tostring method of etree, I get the following output:
<ns0:parent xmlns:ns0="parent-space">
<ns0:child xmlns:ns0="child-space"/>
</ns0:parent>
Both namespace get the label ns0 here and so they clash. How can I avoid this?
There is no clash. The
ns0prefix is just overridden for the descendants of<child>.This XML document
is equivalent to
and
as far as the effective namespaces of
parentandchildgo.You could work with an nsmap to declare prefixes. The effective result is the same, but it looks less confusing when serialized.
this prints