Remove namespace declaration from ElementTree in lxml

Stefan Behnel stefan.behnel-n05pAM at web.de
Sat Dec 29 09:02:16 EST 2007


Zero Piraeus wrote:
>> 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.
> 
> Thanks. I considered copying the tree, but didn't bother trying it as
> I would expect it to be fairly expensive. Maybe no more expensive than
> a regex though. I'll take a look ...

It's not actually copying the tree, but it does create a new document, a new
root element, and then moves the root children over from the original
document, which also involves reference adaptations throughout the entire
tree. So it does not come for free. I'd be interested if you can come up with
numbers how it compares to the string replacement.

Stefan



More information about the Python-list mailing list