Zero Piraeus
2007-12-28 19:28:38 UTC
I want to remove an unused namespace declaration from the root element
of an ElementTree in lxml.
[...] 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 reallyof an ElementTree in lxml.
[...] I'm reduced to sticking the output through str.replace() ...
which is somewhat inelegant.
only occurs where you want to replace it).
' xmlns:someprefix="urn:foo:bar"'
will appear in the content. Not impossible though, I suppose. If I
want to try and make sure I only catch it as an element's attribute,
that leads me to a regex, which seems an even less clever idea.
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 asnew_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.
I would expect it to be fairly expensive. Maybe no more expensive than
a regex though. I'll take a look ...
-[]z.