etree, lxml unexpected behaviour

Stefan Behnel stefan_ml at behnel.de
Wed Jun 17 16:58:06 EDT 2009


Emanuele D'Arrigo wrote:
> Hi everybody, I just tried the following:
> 
>>>> import xml.etree.ElementTree as etree
>>>> e = etree.fromstring('<aRoot xmlns="aNamespace" xmlns:ans="anotherNamespace"><aChild anAttr="1" ans:anotherAttr="2"/></aRoot>')
>>>> e.getchildren()[0].attrib
> {'anAttr': '1', '{anotherNamespace}anotherAttr': '2'}
> 
> Notice the lack of namespace before the attribute "anAttr". I find
> this unexpected because as you can see I did set the default namespace
> with xmlns="aNamespace" and the elements (rather than the attributes)
> are correctly associated with it, i.e.:
> 
>>>> e
> <Element {aNamespace}aRoot at 2326cb0>
> 
> Is there a way to change this behaviour so that attributes without
> namespace are correctly associated with the default namespace, just
> like the elements?

There isn't, because this is the correct behaviour according to the spec.

http://www.w3.org/TR/REC-xml-names/#defaulting

"""
A default namespace declaration applies to all unprefixed element names
within its scope. Default namespace declarations do not apply directly to
attribute names; the interpretation of unprefixed attributes is determined
by the element on which they appear.
"""

Stefan



More information about the Python-list mailing list