[topicmapmail] xpath: stop at first match

G. Ken Holman gkholman at CraneSoftwrights.com
Mon Jul 31 12:25:25 EDT 2006


At 2006-07-31 10:15 -0400, Carlo Moneti wrote:
>On 2006.07.30 15:54 G. Ken Holman wrote:
>>XSLT and XPath questions would be better posted to the following list:
>>   http://www.mulberrytech.com/xsl/xsl-list
>>There are a number of subscribers who would 
>>enthusiastically respond to such questions.
>>There is also an *excellent* XSLT FAQ at:
>>   http://www.dpawson.co.uk
>
>Thanks for the links. But my question is really 
>xpath specific. I'm not using XSL nor XSLT. I am 
>using php5.1.4 and the simpleXML functions---based on libxml2.

Both citations do accommodate XPath questions and 
issues, which is why I included them.

>>If you are using something like "//thing", then 
>>"//thing[1]" will not work for you because it 
>>will find the first child *at every level of 
>>the tree* ... the way around this is 
>>(//thing)[1], but as always, the use of "//" is terribly inefficient.
>
>Right. But my issue is this example (any path will do):
>/topicMap/topic[@id='$id']

Fine ... it is unfortunate you aren't using XSLT 
as the key() facility and function would be very useful here.

>The point is that I know there is only one 
>possible match. So I would like the processor to 
>stop upon finding a match and return the value, 
>rather than needlessly scanning the rest of the document (in this case)

I disagree: the XPath expression you have given 
will only look at 'topic' children element nodes 
of the document element node, and nowhere else in the node tree.

And, anyway, XPath addresses a tree of nodes that 
have been created by the document, so it won't be 
going back to the document itself looking for 
anything, only in the nodes of the XPath tree.

>or the current document fragment.
>I expected there would be a syntax for doing 
>this. I haven't found it. Is there a way?

XPath is declarative and the address you have 
given is addressing *all* topic children with the given id= attribute.

If you added a second predicate as in:

   /topicMap/topic[@id='$id'][1]

this *may* help, but the processor is allowed to 
access them all and then only return the 
first.  A processor that implements lazy 
evaluation *may* stop when it finds the first 
one, but a processor is not obliged to be implemented in a lazy fashion.

The declarative nature of XPath gives 
implementations the freedom to implement the 
solution in any way they see fit ... there are no 
performance "clues" that a user of XPath can give 
to the XPath processor to do its job.  You cannot 
direct an XPath processor on how it gives you its 
result, you can only ask it to return a result however achieved.

I hope this helps.

. . . . . . . . . . . Ken

--
UBL/XML/XSLT/XSL-FO training:         Vårø, Denmark 06-09-25/10-06
World-wide corporate, govt. & user group UBL, XSL, & XML training.
G. Ken Holman                 mailto:gkholman at CraneSoftwrights.com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/t/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Aug'05  http://www.CraneSoftwrights.com/t/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal



More information about the topicmapmail mailing list