[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Remove namespace declaration from ElementTree in lxml

Zero Piraeus

12/28/2007 2:33:00 AM

:

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.

Is there a better way?

-[]z.
3 Answers

Stefan Behnel

12/28/2007 8:48:00 AM

0

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

Zero Piraeus

12/28/2007 7:29:00 PM

0

:

> > 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 really
> only occurs where you want to replace it).

"Nada es seguro" ... but it does seem unlikely that

' 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.

> 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 ...

-[]z.

Stefan Behnel

12/29/2007 2:02:00 PM

0

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