<div>Thanks Stefan,<br><br>All the nodes in that tree should have the same type, that&#39;s why the default class lookup scheme for parser works fine.<br><br>BTW, I have one more question, to set the xml:id i use the following construct:<br>
<br>def xml_id(v): <br>&nbsp;&nbsp;&nbsp; # helper function to create name space attributes <br>&nbsp;&nbsp;&nbsp; return {&#39;{<a href="http://www.w3.org/XML/1998/namespace}id">http://www.w3.org/XML/1998/namespace}id</a>&#39;: v} <br><br>and the following construct:<br>
<br>N.child1(&quot;text&quot;,xml_id(&quot;some_id&quot;))<br><br>following the examples from the site.<br><br>to get the id I use:<br><br>class NodeBase(etree.ElementBase):<br>&nbsp;&nbsp;&nbsp; ...&nbsp;&nbsp;&nbsp;  <br>&nbsp;&nbsp;&nbsp; def get_node_id(self,id):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; searched = self.find(&quot;.//*[@{<a href="http://www.w3.org/XML/1998/namespace}id=&#39;%s&#39;">http://www.w3.org/XML/1998/namespace}id=&#39;%s&#39;</a>]&quot;%(id,))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if searched is None:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise NodeNotFoundError(id)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return searched<br><br><br>I have two questions:<br><br>1. what way is faster to get the element by Id? should I use find or xpath to achieve the better performance?<br>2. is there a way to set xml:id using xml - prefix?<br>
<br>Thanks,<br>Alex<br><br></div>
<div class="gmail_quote">2008/5/2 Stefan Behnel &lt;<a href="mailto:stefan_ml@behnel.de" target="_blank">stefan_ml@behnel.de</a>&gt;:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">Hi,<br><br>another bit of reasoning here.<br>
<div><br>Stefan Behnel wrote:<br>&gt; Alex Klizhentas wrote:<br>&gt;&gt; I&#39;ve extended the ElementBase object using the approach described in the<br>&gt;&gt; tutorial, but SubElement does not work as desired:<br>
&gt;&gt;<br>&gt;&gt; class NodeBase(etree.ElementBase):<br>&gt;&gt; &nbsp; &nbsp; &nbsp;def append(self,child):<br>&gt;&gt; &nbsp;print &quot;aaa&quot;<br>&gt;&gt; &nbsp;return etree.ElementBase.append(self,child)<br>&gt;&gt;<br>&gt;&gt; etree.SubElement(root,&quot;child&quot;) #no &quot;aaa&quot; printed<br>

&gt;<br>&gt; That&#39;s because SubElement() does not call .append().<br></div>[...]<br>
<div>&gt; SubElement() does not call .makeelement() either. It&#39;s implemented in plain C.<br><br></div>One important reason is that this allows lxml.etree to append the new libxml2<br>node at the C level *before* the decision is taken which Python class should<br>

be used to represent it. This might have an impact on the class lookup if it<br>considers the parental relation when taking its decision (lxml.objectify does<br>that, for example).<br><br>But that&#39;s the only difference I can see between etree.SubElement() and your<br>

Python implementation. And you could even work around it by doing something<br>like this:<br>
<div><br>def SubElement(parent, tag, attrib={}, **extra):<br>&nbsp; &nbsp; attrib = attrib.copy()<br>&nbsp; &nbsp; attrib.update(extra)<br>&nbsp; &nbsp; element = parent.makeelement(tag, attrib)<br>&nbsp; &nbsp; parent.append(element)<br></div>&nbsp; &nbsp; del element<br>

&nbsp; &nbsp; return parent[-1]<br><br>However, you might want to avoid that if you know you won&#39;t need it, e.g. when<br>using the &quot;namespace&quot; or &quot;default&quot; lookup scheme.<br><font color="#888888"><br>Stefan<br>

<br></font></blockquote></div><br><br clear="all"><br>-- <br>Regards,<br>Alex