我正在尝试完成一个关于使用BaseX GUI和mondial数据库查找有关过境数量最远的国家(-ies)的学校作业问题。我用BFS编写了一个递归函数来完成它。查询应返回一组与输入国家最远的国家/地区名称。
当我打电话给下面这个函数找到最遥远的尼加拉瓜国家时,应该返回一些国家名称,包括“加拿大”和“阿根廷”。
let $return-list := local:reachable-far($country[name="Nicaragua"])
return $return-list/country/name
但是,我的回报价格只有加拿大,但阿根廷或苏里南没有其他国家。
<name>Canada</name>
我尝试通过调用
来调试查询let $return-list := local:reachable-far($country[name="Nicaragua"])
return $return-list/country[name="Argentina"]/name
返回值为
<name>Argentina</name>
我认为这意味着阿根廷(和其他国家)的回报价值但不知何故不显示。我的代码在这里出了什么问题?完整的功能声明粘贴在下面。
declare variable $mondial := doc("D:/mondial.xml")/mondial;
declare variable $country := $mondial/country;
declare function local:reachable-far($curr)
{
if ($curr = ()) then ()
else
(
local:reachable-bfsl($curr, $curr, $curr, $curr, 0)
)
};
declare function local:reachable-bfsl($queue, $seen, $lastoflevel, $currlevel, $depth)
{
(:return current level if stack is empty (should never happen):)
if (empty($queue)) then (<row>{$currlevel} <depth>{$depth}</depth></row>)
else
(
let $curr := $queue[1]
let $neighbors-all := $curr/border/@country
let $neighbors-code := $neighbors-all[not(.=$seen/@car_code)]
let $neighbors := $country[@car_code = $neighbors-code]
(:if current country is not the last of level, continue with current level:)
return if ($curr/@car_code != $lastoflevel/@car_code)
then
(
local:reachable-bfsl(($queue[position()>1], $neighbors),
($seen, $neighbors),
$lastoflevel,
$currlevel union $curr,
$depth)
)
(:if current country is the last of level:)
else
(
(:current contury has searchable neighbors or stack is not empty after popping:)
if (not(empty($neighbors)) or not(empty($queue[position()>1])))
then
( (:clear current level, continue next level, update last of level to last in stack, depth++:)
local:reachable-bfsl(($queue[position()>1], $neighbors),
($seen, $neighbors),
($queue[position()>1], $neighbors)[last()],
(),
$depth + 1)
)
(:current country does not have searchable neighbors and stack is empty after popping:)
else
(
<row>{trace($currlevel union $curr)} <depth>{$depth}</depth></row>
)
)
)
};
let $return-list := local:reachable-far($country[name="Nicaragua"])
return $return-list/country/name
答案 0 :(得分:0)
在BaseX 9.0.1(当前版本,可能是您正在使用的版本)中,节点ID排序算法有一个小错误。 BaseX 9.0或最新快照(http://files.basex.org/releases/latest/)应该产生预期的结果。 BaseX 9.0.2将于2018年5月底上市。