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