lxml etree question

J

Jim

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
 
A

Adam Tauno Williams

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>
 
S

Stefan Behnel

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
 
J

Jim

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
 
C

Carl Banks

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'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
 
S

Stefan Behnel

Jim, 26.12.2010 00:32:
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. '<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
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top