sql - xQuery 函数返回值未显示
问题描述
我正在尝试完成一个学校作业问题,该问题是关于使用 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
解决方案
在 BaseX 9.0.1(这是当前版本,可能是您正在使用的版本)中,节点 ID 排序算法有一个小错误。BaseX 9.0 或最新快照 ( http://files.basex.org/releases/latest/ ) 应该会产生预期的结果。BaseX 9.0.2 将于 2018 年 5 月底推出。
推荐阅读
- angular - 如何清理动态创建的类实例?
- scala - 错误 StandaloneSchedulerBackend:应用程序已被终止。原因:所有高手都没有反应!放弃。在 CRON 中运行 Spark 时
- python - 在机器学习中使用 2 列训练数据
- python - 如何在 qvboxlayout 中移动小部件
- node.js - 在 NodeJs 中使用 Docker-compose 进行热重载
- python - 基于 Tesseract 的边界框重建文本
- c# - 如何在不使用 EF 代码优先方法创建迁移的情况下使用现有表
- javascript - 如何使 jsonschema 可选?
- mysql - 如何在 wordpress 中使用自定义数据库?
- python - 如何使用正则表达式删除子字符串的结尾?