etree, lxml unexpected behaviour

E

Emanuele D'Arrigo

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.:
<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? lxml exhibit the exact same behaviour.

Manu
 
S

Stefan Behnel

Emanuele said:
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.:
<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top