Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

lxml question -- creating an etree.Element attribute with ':' in the name

1,466 views
Skip to first unread message

Roy Smith

unread,
Sep 18, 2013, 2:59:19 PM9/18/13
to Python List
I can create an Element with a 'foo' attribute by doing:

etree.Element('my_node_name', foo="spam")

But, how do I handle something like:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", since "xmlns:xsi" isn't a valid python identifier?

---
Roy Smith
r...@panix.com



Zachary Ware

unread,
Sep 18, 2013, 3:07:33 PM9/18/13
to Roy Smith, Python List
On Wed, Sep 18, 2013 at 1:59 PM, Roy Smith <r...@panix.com> wrote:
> I can create an Element with a 'foo' attribute by doing:
>
> etree.Element('my_node_name', foo="spam")
>
> But, how do I handle something like:
>
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", since "xmlns:xsi" isn't a valid python identifier?
>

Try this:

etree.Element('my_node_with_invalid_identifiers_name', **{'xmlns:xsi': 'spam'})

HTH,

--
Zach

Burak Arslan

unread,
Sep 18, 2013, 3:35:10 PM9/18/13
to Roy Smith, Python List
On 09/18/13 21:59, Roy Smith wrote:
> I can create an Element with a 'foo' attribute by doing:
>
> etree.Element('my_node_name', foo="spam")
>
> But, how do I handle something like:
>
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", since "xmlns:xsi" isn't a valid python identifier?
>
>

xmlns: is a prefix with a special meaning: it defines an xml namespaces
prefix. you should read about how they work.

The following:

Element('{http://www.w3.org/2001/XMLSchema-instance}my_node_name')

will generate a proper xmlns declaration for you. It may not be the same
every time, but it will do the job just as well.

btw, if you need to generate xml schemas, have a look at spyne:
http://spyne.io

Specifically:
https://github.com/arskom/spyne/blob/master/examples/xml/schema.py

best,
burak

dieter

unread,
Sep 19, 2013, 2:10:42 AM9/19/13
to pytho...@python.org
Roy Smith <r...@panix.com> writes:

> But, how do I handle something like:
>
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", since "xmlns:xsi" isn't a valid python identifier?

Read about "lxml"'s "namespace" support.

Stefan Behnel

unread,
Sep 19, 2013, 1:35:52 PM9/19/13
to pytho...@python.org
Burak Arslan, 18.09.2013 21:35:
> On 09/18/13 21:59, Roy Smith wrote:
>> I can create an Element with a 'foo' attribute by doing:
>>
>> etree.Element('my_node_name', foo="spam")
>>
>> But, how do I handle something like:
>>
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", since "xmlns:xsi" isn't a valid python identifier?
>
> xmlns: is a prefix with a special meaning: it defines an xml namespaces
> prefix. you should read about how they work.

Absolutely. For the specific case of a namespaced attribute, one way to do
it goes like this:

el = etree.Element('my_node_name')
el.set('{http://www.w3.org/2001/XMLSchema-instance}xsi', 'int')


> The following:
>
> Element('{http://www.w3.org/2001/XMLSchema-instance}my_node_name')
>
> will generate a proper xmlns declaration for you. It may not be the same
> every time, but it will do the job just as well.

For this specific namespace, and also a couple of other well-known
namespace URIs, lxml will use the "expected" prefix by default.

Stefan


gere...@gmail.com

unread,
Dec 2, 2019, 11:58:28 PM12/2/19
to
Theanks a lot

Karsten Hilbert

unread,
Dec 3, 2019, 11:01:04 AM12/3/19
to
On Mon, Dec 02, 2019 at 08:58:11PM -0800, gere...@gmail.com wrote:
> Date: Mon, 2 Dec 2019 20:58:11 -0800 (PST)
> From: gere...@gmail.com
> To: pytho...@python.org
> Subject: Re: lxml question -- creating an etree.Element attribute with ':'
> in the name
> User-Agent: G2/1.0
>
> Theanks a lot
> --
> https://mail.python.org/mailman/listinfo/python-list

you are welcoem

--
GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B
0 new messages