Remove namespace declaration from ElementTree in lxml
Stefan Behnel
stefan.behnel-n05pAM at web.de
Fri Dec 28 03:47:42 EST 2007
Zero Piraeus wrote:
> I want to remove an unused namespace declaration from the root element
> of an ElementTree in lxml.
>
> There doesn't seem to be any documented way to do this, so at the
> moment I'm reduced to sticking the output through str.replace() ...
> which is somewhat inelegant.
And also a bit error prone (unless you are sure the replaced string really
only occurs where you want to replace it). You can try this:
root = etree.parse(...).getroot()
new_root = etree.Element(root.tag, root.attrib)
new_root[:] = root[:]
Note, however, that this will not copy root-level PIs or internal DTD subsets.
But you can copy PIs and comments by hand.
Stefan
More information about the Python-list
mailing list