首页 > 解决方案 > 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

标签: sqlxmldatabasexquerybasex

解决方案


在 BaseX 9.0.1(这是当前版本,可能是您正在使用的版本)中,节点 ID 排序算法有一个小错误。BaseX 9.0 或最新快照 ( http://files.basex.org/releases/latest/ ) 应该会产生预期的结果。BaseX 9.0.2 将于 2018 年 5 月底推出。


推荐阅读