[lxml-dev] Proper syntax to insert elements with namespaces?

Mike MacCana mmaccana at au1.ibm.com
Tue Jul 15 08:09:49 CEST 2008


On Wed, 2008-07-09 at 09:17 +0200, Stefan Behnel wrote:


> > 2. How do I insert an element with a namespace? The code above creates:
> >       <text name="Standard">new element text</text>
> > 
> > But I'd prefer: 
> >       <text:p text:style-name="Standard">old element</text:p>
> 
> http://codespeak.net/lxml/tutorial.html#namespaces
> 
> Stefan

I should have been clearer: it's not the element with a namespace that's
a prob (thanks to the tutorial above) but the attribute with the
namespace.

I currently have:

text_element =
etree.Element("{urn:oasis:names:tc:opendocument:xmlns:text:1.0}p",stylename='Standard')
text_element.text = "new text"
body[0].insert(1, text_element)

Which generates <text:p stylename="Standard">new text</text:p>

- I haven't been able to find any examples of setting a namespace to
apply to an attribute - so that the stylename attribute has the text
namespace (as OpenDocument seems to do things).

- Additionally If I set the attribute to be style-name instead of
stylename, I get
'SyntaxError: keyword can't be an expression'. 
OK, I understand that. But I when I quote this so the dash comes out
literally, lxml complains about invalid syntax. 

Any ideas? I've had a hunt around and not been able to find any examples
for attribute namespaces or attributes with special characters...

Thanks again,

Mike

________________________________________________
Mike MacCana
Technical Specialist
Australia Linux and Virtualisation Services

IBM Global Services
Level 14, 60 City Rd
Southgate Vic 3000 

Phone: +61-3-8656-2138
Fax: +61-3-8656-2423
Email: mmaccana at au1.ibm.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://codespeak.net/pipermail/lxml-dev/attachments/20080715/dbce929b/attachment.htm 


More information about the lxml-dev mailing list