RE: [xml] XPath / LibXML question


As Aron already pointed out, the position() should be of
help. You need to select the descendant-or-self or descendant
Your expression could look like this:
"//instance[ att2='2' and parent::instances/parent::item[ att1='1']][1]"

But a test revealed that this is not optimized in Libxml2.
Although there's the function xmlXPathNodeCollectAndTestNth(), which
is designed to optimize a "position()" predicate, this function is
also designed to be fired only if there's no previous predicate;
so //instance[1] would be optimized, but not //instance[some

I'll try to look into this.



-----Original Message-----
From: xml-bounces gnome org [mailto:xml-bounces gnome org] On 
Behalf Of Aron Stansvik
Sent: Thursday, June 01, 2006 4:19 PM
To: xml gnome org
Subject: Re: [xml] XPath / LibXML question

On 6/1/06, Alan_McCarthy scee net <Alan_McCarthy scee net> wrote:


I have a question about XPath and have been pointed in the 
direction of
this mailing list. Hope this is the correct place.

I have the following XPath:

/base/group/item[ att1=1]/instances/instance[ att2=2]

and when used on my XML document it returns multiple items, e.g.

/base[1]/group[1]/item[167]/instances[1]/instance[1] - result
/base[1]/group[1]/item[195]/instances[1]/instance[1] - result
/base[1]/group[1]/item[197]/instances[1]/instance[1] - result
/base[1]/group[1]/item[202]/instances[1]/instance[1] - result
/base[1]/group[1]/item[216]/instances[1]/instance[1] - result

In this particular instance however, I only need to find one of the
results, and it doesn't matter which one.
For CPU efficiency I would like to find a way to do that.
Does anyone know how I can achieve this, ideally by  
modifying my XPath,
but if that is not possible, if there are parameters, or 
context set up
that could be done within LibXML to restrict it to one result.

You could use a predicate such as [position() = 1] to get the first
result. I don't know if this saves you anything performance-wise
though, one of the libxml2 hackers will have to answer that.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]