Leo,
;-) Ausgezeichnet (briljant)
Exactly what I was looking for. I don't know if I had been able to figure this out on my own, not within reasonable time. Not even with Christian's tips. For me it's a whole new concept.
Danke,
Rob
-----Oorspronkelijk bericht----- Van: Leonard Wörteler [mailto:leo@woerteler.de] Verzonden: donderdag 19 juli 2012 17:28 Aan: Rob CC: basex-talk@mailman.uni-konstanz.de Onderwerp: Re: [basex-talk] Question concerning querying cyclic network xml-structure.
Dear Rob,
Am 19.07.2012 07:24, schrieb Rob:
You're probably right. I hoped to find a working example somewhere. But, I still haven't found what I was looking for (this would make a good song title) and that worries me somehow. I'll have a look at the links and do some more digging.
it sounds to as if you want to implement a depth-first search over your documents. That's actually pretty easy in XQuery. The trick is to simulate the mutable state (in your case the global variable `Guard`) by threading it through all resursive function calls.
In the attached example I used an example graph from Wikipedia [1]. It is encoded as a list of nodes and references to their children:
<graph> <node name='Frankfurt' id='1'> <child idref='2' /> <child idref='3' /> <child idref='4' /> </node> ... </graph
The graph obviously has many cycles. the function `local:depth-first($graph, $start)` traverses it in depth-first order, visiting every node only once and collecting the node names in order.
I used the Map Module [2] to encapsulate the state and the XQuery 3.0 higher-order function `fold-left(..)` [3], but you could use your own recursive function instead of the latter to iterate over the child nodes and pass the state along.
Hope this helps, cheers, Leo __________
[1] http://en.wikipedia.org/wiki/File:MapGermanyGraph.svg [2] http://docs.basex.org/wiki/Map_Module [3] http://docs.basex.org/wiki/Higher-Order_Functions#Folds