lxml etree question

Discussion in 'Python' started by Jim, Dec 24, 2010.

  1. Jim

    Jim Guest

    Hello, I wonder if someone knows about lxml.etree and namespaces?

    I want to build an ElementTree where some of the sub-elements have
    attributes that serialize this way.

    <comment xml:lang='de'>..</comment>

    I've tried just comment_elet.set('xml:lang','de') and it didn't like
    that at all (although it takes comment_elet.set('auth:id','jones')
    just fine). I've also spelunked the docs and googled but have not hit
    on the right invocation. If someone knows, I'd be grateful.

    Jim
    Jim, Dec 24, 2010
    #1
    1. Advertising

  2. On Fri, 2010-12-24 at 20:48 +0530, Nitin Pawar wrote:
    > On Fri, Dec 24, 2010 at 8:40 PM, Jim <> wrote:
    > Hello, I wonder if someone knows about lxml.etree and
    > namespaces?


    Yes, and don't.

    > I want to build an ElementTree where some of the sub-elements have
    > attributes that serialize this way.
    > <comment xml:lang='de'>..</comment>
    > I've tried just comment_elet.set('xml:lang','de') and it didn't like
    > that at all (although it takes comment_elet.set('auth:id','jones')
    > just fine). I've also spelunked the docs and googled but have not hit
    > on the right invocation. If someone knows, I'd be grateful.


    I'd *strongly* recommend using ElementFlow for building XML documents
    (over ElementTree), especially if namespaces are involved. ElementFlow
    is far more intuitive.
    <http://pypi.python.org/pypi/elementflow>
    Adam Tauno Williams, Dec 24, 2010
    #2
    1. Advertising

  3. Jim, 24.12.2010 16:10:
    > Hello, I wonder if someone knows about lxml.etree and namespaces?
    >
    > I want to build an ElementTree where some of the sub-elements have
    > attributes that serialize this way.
    >
    > <comment xml:lang='de'>..</comment>
    >
    > I've tried just comment_elet.set('xml:lang','de') and it didn't like
    > that at all (although it takes comment_elet.set('auth:id','jones')
    > just fine). I've also spelunked the docs and googled but have not hit
    > on the right invocation. If someone knows, I'd be grateful.


    This should get you on the right track:

    http://codespeak.net/lxml/tutorial.html#namespaces

    In short: you need to distinguish between namespaces (URIs) and namespace
    prefixes. ElementTree and lxml.etree use the fully qualified tag name in
    the form "{namespace-URI}localname}".

    As for the special case of the "xml" prefix, the XML namespace spec has
    this to say:

    """
    The prefix xml is by definition bound to the namespace name
    http://www.w3.org/XML/1998/namespace. It MAY, but need not, be declared,
    and MUST NOT be bound to any other namespace name. Other prefixes MUST NOT
    be bound to this namespace name, and it MUST NOT be declared as the default
    namespace.
    """

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

    lxml knows about this special case, so you can write

    {http://www.w3.org/XML/1998/namespace}lang

    and lxml will take care of using the right prefix.

    Stefan
    Stefan Behnel, Dec 25, 2010
    #3
  4. Jim

    Jim Guest

    On Dec 25, 5:33 am, Stefan Behnel <> wrote:
    > lxml knows about this special case, so you can write
    >
    >         {http://www.w3.org/XML/1998/namespace}lang
    >
    > and lxml will take care of using the right prefix.


    Stefan, thank you for the software, which has helped me a great deal.

    I tried that exact thing, among a number of others, and it didn't work
    for me (I got ns0). I'll try again, this time with the understanding
    that it is the right one, and so perhaps I may discover some small
    error in what I tried.

    Regards, and thanks again,
    Jim
    Jim, Dec 25, 2010
    #4
  5. Jim

    Carl Banks Guest

    On Dec 24, 10:17 am, Adam Tauno Williams <>
    wrote:
    > On Fri, 2010-12-24 at 20:48 +0530, Nitin Pawar wrote:
    > > On Fri, Dec 24, 2010 at 8:40 PM, Jim <> wrote:
    > >         Hello,  I wonder if someone knows about lxml.etree and
    > >         namespaces?

    >
    > Yes, and don't.


    He's using lxml.etree (which is a third-party library that mimics
    ElementTree's interface), not ElementTree. Were you aware of this?


    > > I want to build an ElementTree where some of the sub-elements have
    > > attributes that serialize this way.
    > > <comment xml:lang='de'>..</comment>
    > > I've tried just comment_elet.set('xml:lang','de') and it didn't like
    > > that at all (although it takes comment_elet.set('auth:id','jones')
    > > just fine).  I've also spelunked the docs and googled but have not hit
    > > on the right invocation.  If someone knows, I'd be grateful.

    >
    > I'd *strongly* recommend using ElementFlow for building XML documents
    > (over ElementTree), especially if namespaces are involved.  ElementFlow
    > is far more intuitive.
    > <http://pypi.python.org/pypi/elementflow>


    I'd have to disagree with the use of strong recommendation here. The
    library you recommended isn't a general replacement for lxml (or
    ElementTree), and you didn't qualify the conditions for when it is a
    suitable alternative.

    A. What if he needed to keep the tree in memory?
    B. This library builds the tags with "with" statements, which could be
    convenient for xml files with rigid structure, but I would think it'd
    be inconvenient if the format were relatively loose.

    If you're going to recommend a more specialized solution, you should
    also give the conditions for which it is suitable.


    Carl Banks
    Carl Banks, Dec 26, 2010
    #5
  6. Jim, 26.12.2010 00:32:
    > On Dec 25, 5:33 am, Stefan Behnel wrote:
    >> lxml knows about this special case, so you can write
    >>
    >> {http://www.w3.org/XML/1998/namespace}lang
    >>
    >> and lxml will take care of using the right prefix.

    >
    > Stefan, thank you for the software, which has helped me a great deal.
    >
    > I tried that exact thing, among a number of others, and it didn't work
    > for me (I got ns0).


    Works for me, at least with a recent SVN version:

    Python 2.7.1rc1+ (trunk:86636, Nov 21 2010, 09:18:37)
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import lxml.etree as ET
    >>> el = ET.Element('test',

    ... {'{http://www.w3.org/XML/1998/namespace}lang': 'de'})
    >>> ET.tostring(el)

    '<test xml:lang="de"/>'

    Anyway, I applied a patch that makes sure it will always use the 'xml'
    prefix for this namespace. Will be in 2.3 final.

    Stefan
    Stefan Behnel, Dec 26, 2010
    #6
    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. Roy Smith
    Replies:
    0
    Views:
    108
    Roy Smith
    Sep 18, 2013
  2. Zachary Ware
    Replies:
    0
    Views:
    106
    Zachary Ware
    Sep 18, 2013
  3. Burak Arslan
    Replies:
    0
    Views:
    121
    Burak Arslan
    Sep 18, 2013
  4. dieter
    Replies:
    0
    Views:
    102
    dieter
    Sep 19, 2013
  5. Stefan Behnel
    Replies:
    0
    Views:
    107
    Stefan Behnel
    Sep 19, 2013
Loading...

Share This Page