LXML: can't register namespace

Stefan Behnel stefan_ml at behnel.de
Fri Mar 9 09:46:06 EST 2018


Peter Otten schrieb am 09.03.2018 um 14:11:
> Stefan Behnel wrote:
> 
>> Andrew Z schrieb am 07.03.2018 um 05:03:
>>> Hello,
>>>  with 3.6 and latest greatest lxml:
>>>
>>> from lxml import etree
>>>
>>> tree = etree.parse('Sample.xml')
>>> etree.register_namespace('','http://www.example.com')
>>
>> The default namespace prefix is spelled None (because there is no prefix
>> for it) and not the empty string.
> 
> Does that mean the OP shouldn't use register_namespace() at all or that he's 
> supposed to replace "" with None?

It meant neither of the two, but now that you ask, I would recommend the
first. ;)

An application global setup for the default namespace is never a good idea,
thus my question regarding the actual intention of the OP. Depending on the
context, the right thing to do might be be to either not care at all, or to
not use the default namespace but a normally prefixed one instead, or to
define a (default) namespace mapping for a newly created tree, as shown in
the namespace tutorial.

http://lxml.de/tutorial.html#namespaces

Usually, not caring about namespace prefixes is the best approach. Parsers,
serialisers and compressors can deal with them perfectly and safely, humans
should just ignore the clutter, pitfalls and complexity that they introduce.

Stefan




More information about the Python-list mailing list