lxml etree question
Stefan Behnel
stefan_ml at behnel.de
Sat Dec 25 05:33:20 EST 2010
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
More information about the Python-list
mailing list