<div>Thanks Stefan,<br><br>All the nodes in that tree should have the same type, that'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> # helper function to create name space attributes <br> return {'{<a href="http://www.w3.org/XML/1998/namespace}id">http://www.w3.org/XML/1998/namespace}id</a>': v} <br><br>and the following construct:<br>
<br>N.child1("text",xml_id("some_id"))<br><br>following the examples from the site.<br><br>to get the id I use:<br><br>class NodeBase(etree.ElementBase):<br> ... <br> def get_node_id(self,id):<br>
searched = self.find(".//*[@{<a href="http://www.w3.org/XML/1998/namespace}id='%s'">http://www.w3.org/XML/1998/namespace}id='%s'</a>]"%(id,))<br> if searched is None:<br> raise NodeNotFoundError(id)<br>
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 <<a href="mailto:stefan_ml@behnel.de" target="_blank">stefan_ml@behnel.de</a>>:<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>> Alex Klizhentas wrote:<br>>> I've extended the ElementBase object using the approach described in the<br>>> tutorial, but SubElement does not work as desired:<br>
>><br>>> class NodeBase(etree.ElementBase):<br>>> def append(self,child):<br>>> print "aaa"<br>>> return etree.ElementBase.append(self,child)<br>>><br>>> etree.SubElement(root,"child") #no "aaa" printed<br>
><br>> That's because SubElement() does not call .append().<br></div>[...]<br>
<div>> SubElement() does not call .makeelement() either. It'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'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> attrib = attrib.copy()<br> attrib.update(extra)<br> element = parent.makeelement(tag, attrib)<br> parent.append(element)<br></div> del element<br>
return parent[-1]<br><br>However, you might want to avoid that if you know you won't need it, e.g. when<br>using the "namespace" or "default" lookup scheme.<br><font color="#888888"><br>Stefan<br>
<br></font></blockquote></div><br><br clear="all"><br>-- <br>Regards,<br>Alex