[lxml-dev] Namespace handling problems in LXML 1.1.1

Lee Brown lee.brown at elecdev.com
Mon Jan 15 18:45:56 CET 2007


Greetings!

I've run into a few snags related to namespace handling in LXML 1.1.1 (I'm using
the bundled Windows distribution).

First, given an lxml tree object created from an xml file with a root element
that goes something like this:

<rootelement xmlns="ns1" xmlns:fred="ns2" xmlns:bob="ns3">

One might expect that the namespace definitions would be preserved upon
reserialization.  Unfortunately, they are not; what you get using the tostring()
function is just <rootelement>.  Is there some function or keyword in lxml that
will case the namespace definitions to be re-mapped into the output?  I've tried
to find such a thing if it exists using the docstrings and introspection but
I've come up empty-handed.

Second, one might think that the nsmap attribute would be 'just the ticket' for
performing xpath searches:

elements = mytree.xpath('//fred:someelement', mytree.getroot().nsmap)

But this fails, because (following from the first example above) the nsmap
attribute for <rootelement> yeilds

{None:'ns1', 'fred':'ns2', 'bob':'ns3'}

Note: the above xpath search succeeds if the None:'ns1' namespace is deleted
from nsmap.

Using a NoneType as a key in nsmap causes it to be unusable as far as the xpath
function is concerned (and probably some other places as well.)  Is there any
workaround, other than creating (and updating) my own namespace dictionary
within my code?

Finally, if someone wants to suggest any 'best practices' for working with
namespaces in lxml, I'd be very interested in reading them.



More information about the lxml-dev mailing list