[topicmapmail] xpath: stop at first match

Carlo Moneti cmoneti at twcny.rr.com
Mon Jul 31 13:33:41 EDT 2006


On 2006.07.31 12:23 Lars Marius Garshol wrote:
> 
> * Carlo Moneti
>> 
>> Right. But my issue is this example (any path will do):
>> /topicMap/topic[@id='$id']
> 
> If you want to do this you should use xsl:key, if you are in XSLT.
> 
> But in any case it is *very* likely that you are doing something  
> that's not really a good idea. Processing XTM with XSLT/XPath is,  
> well, not a good idea. It's extremely painful, you are pretty much  
> guaranteed not to interpret the XTM document correctly, and it really  
> brings you none of the benefits of using Topic Maps in the first  
> place, since you then have to reimplement all of the functionality  
> yourself.

I am not using XSLT (or XSL). Yes, totally non-scalable. I agree with you 
on every point.

> So I strongly recommend using a Topic Maps engine instead of working  
> directly on the XTM.

I fully agree. The application will run on a database backend (still 
exploring DB tools and datamodels). But it also reads topic maps directly 
for testing and other purposes.
> 
>> 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) or the current document fragment. I expected there  would be a 
>> syntax for doing this. I haven't found it. Is there a way?
> 
> No, there isn't. The reason is that all (or nearly all) XPath  
> implementations build a full document tree before they start  
> evaluating the XPath expression, and so by the time the XPath is  
> executed the whole document has already been read.

Right. But once the tree is built, I have to thrash through it NUMEROUS 
times. Using php's simpleXML functions, I have access to either an xpath 
construct or an iterator that treats the tree as a php multi-dimensional 
array which allows me to navigate via foreach() loop. I found that when 
I'm looking for a unique path, xpath will always scan the whole tree; 
with foreach() I can of course stop when I've found a match. The speedup 
is quite big overall. My solution is to use foreach() when searching a 
unique value, and xpath for all other searches (code is smaller, cleaner, 
and faster). It would be nice to use the xpath for both cases for cleaner 
code.

It's a shame there isn't an option for an xpath scan to stop after one 
match. Surely my case isn't that unique? Perhaps I'm missing something.

Thank you,
Carlo



More information about the topicmapmail mailing list