Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54432 > unrolled thread
| Started by | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| First post | 2013-09-19 19:35 +0200 |
| Last post | 2013-09-19 19:35 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: lxml question -- creating an etree.Element attribute with ':' in the name Stefan Behnel <stefan_ml@behnel.de> - 2013-09-19 19:35 +0200
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2013-09-19 19:35 +0200 |
| Subject | Re: lxml question -- creating an etree.Element attribute with ':' in the name |
| Message-ID | <mailman.156.1379612165.18130.python-list@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
Back to top | Article view | comp.lang.python
csiph-web