【发布时间】:2018-05-12 15:14:21
【问题描述】:
我正在尝试完成一个学校作业问题,该问题是关于使用 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
【问题讨论】:
-
在我看来,问题可能出在您调用查询和显示结果的方式上,而您没有告诉我们任何相关信息。您也可以尝试使用
return <out>{$return-list/country/name}</out>将结果包装到单个元素中 -
我已经在 oXygen 中使用 XQuery 评估器和 Saxon 作为 XQuery 处理器,然后在 BaseX GUI 中尝试了您的查询,两者都返回相同的 9 个国家/地区名称序列。
-
这可能与 BaseX 的特定版本有关(我猜它发生在 9.0.1 上?)。我知道更多后会尽快回复您。
标签: sql xml database xquery basex