[lxml-dev] Proper syntax to insert elements with namespaces?
Stefan Behnel
stefan_ml at behnel.de
Tue Jul 15 12:16:23 CEST 2008
Mike MacCana wrote:
> 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.
Same solution for both:
ns = "{...}"
Element(ns+"p", attrib={ns+"style-name" : "Standard"})
(and I admit that that's missing from the tutorial)
Stefan
More information about the lxml-dev
mailing list