lxml namespaces problem

Stefan Behnel stefan.behnel-n05pAM at web.de
Mon Jan 8 10:15:00 EST 2007


Maxim Sloyko wrote:
> I have a little problem with XML namespaces.
> In my application I have two XML processors, that process the same
> document, one after the other.  The first one looks for nodes in 'ns1'
> namespace, and substitutes them, according to some algorithm. After
> this processor is finished, it is guaranteed that there are no more
> 'ns1' nodes left in the tree. however 'ns1' namespace dclaration is
> still
> there, in the root node (well, I put it there manually). Now, when
> this namespace is no longer needed, I want to get rid of it, because
> it confuses some other processors (namely, my browser)
> 
> So, the question is, how do I do that?
> del tree.getroot().nsmap['ns1']
> does not seem to do the trick :(

Please ask this kind of questions on the lxml mailing list. You might also
want to consider searching the mail archive first.

First of all, lxml produces perfectly well-formed XML here. There is no reason
a document should not contain any unused namespace declarations. It's the
browser that's broken if it handles the namespace incorrectly.

Then: the thing is that lxml can't know that you removed all occurrences of
the respective namespace, so it can't just remove the declaration by itself.
lxml 1.2 will likely be able to handle these things a little (!) more
beautifully, but it's not there yet.

One way to get around this might be to reimplement the namespace replacement
in XSLT. You might also want to try to copy the elements to a newly created
document that does not have the original namespace declaration. Note, however,
that this is not guaranteed to work and might break depending on the lxml version.

Stefan



More information about the Python-list mailing list