etree, lxml unexpected behaviour

Discussion in 'Python' started by Emanuele D'Arrigo, Jun 17, 2009.

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

    Manu
     
    Emanuele D'Arrigo, Jun 17, 2009
    #1
    1. Advertising

  2. 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
     
    Stefan Behnel, Jun 17, 2009
    #2
    1. Advertising

  3. Thank you for the clarification Stefan, I understand.
     
    Emanuele D'Arrigo, Jun 17, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KLEIN Stéphane
    Replies:
    1
    Views:
    395
    Stefan Behnel
    Aug 25, 2008
  2. TheIvIaxx
    Replies:
    1
    Views:
    1,570
    TheIvIaxx
    Dec 11, 2008
  3. Jim

    lxml etree question

    Jim, Dec 24, 2010, in forum: Python
    Replies:
    5
    Views:
    783
    Stefan Behnel
    Dec 26, 2010
  4. hein
    Replies:
    2
    Views:
    848
    Neil Cerutti
    Jan 27, 2011
  5. Adam Tauno Williams

    etree/lxml/XSLT and dynamic stylesheet variables

    Adam Tauno Williams, Jan 20, 2012, in forum: Python
    Replies:
    0
    Views:
    183
    Adam Tauno Williams
    Jan 20, 2012
Loading...

Share This Page