I'm trying to make an algorithm that searchs and returns a path between two nodes in a graph in xQuery, i've had no luck so far as it returns just one node and it's adyacent nodes.
First i should make clear that the graph is a directed graph and every node can have zero, one or more origins, in the XML a node only has the link to it's origin but not to it's following nodes 
Here's an example of some nodes and their XML
<node>
  <id> 123-456-789</id>
  <name> something </name>
  <Links>
     <Link>
        <origin></origin>
     </Link>
  <Links>
 <node>
  <id> 245-678-901</id>
  <name> node 2</name>
  <Links>
     <Link>
        <origin> 123-456-789 </origin>
     </Link>
  <Links>
  <node>
  <id> xxx-xxx-xxx</id>
  <name> node 3</name>
  <Links>
     <Link>
        <origin> 123-456-789 </origin>
     </Link>
  <Links>
  <node>
  <id> 234-546-768</id>
  <name> node 4</name>
  <Links>
     <Link>
        <origin> 245-678-901</origin>
     </Link>
  <Links>
From that XML i would like to get the path from node 1 to node 4 ( node1-> node2 -> node4) but whatever i try to do would just give me node1-node2 and node3 but not node4 another thing is that i want to select a path that is not direct, i mean, if i want the path between node5 and node7 but both node5 and node7 are directed towards node6
I've tried adapting this python code to xquery
def BFS(graph,start,end,q):
temp_path = [start]
q.enqueue(temp_path)
while q.IsEmpty() == False:
    tmp_path = q.dequeue()
    last_node = tmp_path[len(tmp_path)-1]
    print tmp_path
    if last_node == end:
        print "VALID_PATH : ",tmp_path
    for link_node in graph[last_node]:
        if link_node not in tmp_path:
            new_path = []
            new_path = tmp_path + [link_node]
            q.enqueue(new_path)
(code not mine, it belongs to it's rightful coder at this activestate page)
here is what i've tried to do:
declare function local:BFS($graph as element()* , $ini_node as element(Node)*, $end_node as element(Node)*) as element()*
{
    let $seq := $ini_node
    let $queue := ($seq)
    for $item in $queue
        return
            if ( count($queue) > 0) then
                let $seq := remove($queue, count($queue))
                let $last := $seq[last()] return if (deep-equal($last, $end_node)) then $seq
                else
                    for $node in $graph[contains(.,$graph/id[contains(.,$last/Links/Link/origin/text())])] (: what i've tried was to get the graph nodes which id is equal to the origins of the last node :)
                        return if(not(functx:is-node-in-sequence-deep-equal($node,$seq))) then
                            let $new_path:= ()
                            let $new_path:= insert-before($seq, count($seq)+1, $node)
                            let $queue := insert-before($queue,1, $new_path) return $queue
                        else ()
            else
                ()
};
 
    