[topicmapmail] xpath: stop at first match

Carlo Moneti cmoneti at twcny.rr.com
Tue Aug 1 21:05:29 EDT 2006


On 2006.08.01 19:08 Alexander Johannesen wrote:

> Well, the correct answer is that it doesn't exist as an implicit
> option in *XPath*. However, XPath exist wrapped in many different
> technologies. In this case you're using the SimpleXML wrapper which,
> honestly, isn't that advanced, and I've had heaps of trouble with it
> in the past when doing Topic Maps (for example, it can't handle the
> object transfer where attributes are the same for different elements
> in a list, for example).

I experienced a couple of quirks related to namespace handling, but I got 
around them all.
> . . . for a lot
> of these things you can wrap parts of your solution in XSLT (see PHP5
> and DOMDocuments ; simpleXML can convert between itself and
> DOMDocument, which the XSLT implementation uses) ; it uses XML Schema
> ID and IDREF(s), which means that all elements with id attributes in
> your XML gets an automatic lookup table, which is *exactly* what
> you're after. You use it through the id() function as ;
> 
>   <xsl:value-of select="id($some.id)" />
> 
> (prints out the value for the element with an id you're after)
> 
>> But it seems mighty useful.
> 
> The only thing you can do is say "expression[1]" and find an
> implementation which is smart enough to stop if XPath is the only tool
> you're using. Some do, some don't, and I wouldn't expect this
> behaviour from SimpleXML. :)

I tried using DOMDocument in one version of my class, but I found it 
slower. The best performing solution I've found in PHP is simpleXML using 
xpath for queries that may return more than one item, and the simpleXML 
foreach() iterator for queries on unique values (because I can break on 
first match). A "stop on first match" option in xpath strikes me as a 
highly useful feature. And, in my case, my code would be tighter and more 
consistent. I'm sorry to get confirmation that it doesn't exist.

Thank you all for your comments.

Carlo


More information about the topicmapmail mailing list