Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.07; 'element': 0.07; 'subject: -- ': 0.07; 'default.': 0.09; 'defines': 0.09; 'namespace': 0.09; 'prefix': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:question': 0.10; 'python': 0.11; 'attribute,': 0.16; 'burak': 0.16; 'declaration': 0.16; 'doing:': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'namespace,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'subject:skip:e 10': 0.16; 'well-known': 0.16; 'wrote:': 0.18; 'stefan': 0.19; 'header:User-Agent:1': 0.23; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; 'xml': 0.29; 'work.': 0.31; 'subject:the': 0.34; 'subject:with': 0.35; 'something': 0.35; 'received:84': 0.35; 'but': 0.35; 'url:org': 0.36; 'should': 0.36; 'handle': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'how': 0.40; 'read': 0.60; 'you.': 0.62; 'smith': 0.68; 'special': 0.74; "'foo'": 0.84; 'absolutely.': 0.84; 'received:arcor-ip.net': 0.84; 'received:pools.arcor-ip.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: lxml question -- creating an etree.Element attribute with ':' in the name Date: Thu, 19 Sep 2013 19:35:52 +0200 References: <523A006E.6050101@arskom.com.tr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: dslb-084-056-020-078.pools.arcor-ip.net User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130803 Thunderbird/17.0.8 In-Reply-To: <523A006E.6050101@arskom.com.tr> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379612165 news.xs4all.nl 15966 [2001:888:2000:d::a6]:44238 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54432 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