2 May 19:42
Re: Custom Elements question
From: Stefan Behnel <stefan_ml <at> behnel.de>
Subject: Re: Custom Elements question
Newsgroups: gmane.comp.python.lxml.devel
Date: 2008-05-02 17:42:09 GMT
Subject: Re: Custom Elements question
Newsgroups: gmane.comp.python.lxml.devel
Date: 2008-05-02 17:42:09 GMT
Hi,
Alex Klizhentas wrote:
> I have one more question, to set the xml:id i use the following construct:
>
> def xml_id(v):
> # helper function to create name space attributes
> return {'{http://www.w3.org/XML/1998/namespace}id': v}
>
> and the following construct:
>
> N.child1("text",xml_id("some_id"))
>
> following the examples from the site.
>
> to get the id I use:
>
> class NodeBase(etree.ElementBase):
> ...
> def get_node_id(self,id):
> searched = self.find(".//*[@{
> http://www.w3.org/XML/1998/namespace}id='%s']"%(id,))
> if searched is None:
> raise NodeNotFoundError(id)
> return searched
>
> I have two questions:
>
> 1. what way is faster to get the element by Id? should I use find or xpath
> to achieve the better performance?
timeit will tell you that. But it really depends on the data. element.find()
stops short after the first hit, so that's probably faster on average if the
document is large. OTOH, XPath() is implemented in C and could easily beat the
Python code behind find("..@attr...") for smaller documents...
Try this:
find_id = etree.ETXPath(
".//*[@{http://www.w3.org/XML/1998/namespace}id=$id]")
...
def get_node_id(self,id):
el = find_id(self, id=id)
> 2. is there a way to set xml:id using xml - prefix?
No, but if you know you run single-threaded, you can reuse the attrib dict and
just change the value. That's faster than recreating it each time.
Stefan
RSS Feed