我正在尝试使用 mondial.sql 数据库通过陆路边界从一个国家/地区穿越到另一个国家/地区,从而找到陆路可到达的所有国家/地区。它必须递归完成,我在网上找到了一些函数,我认为这些函数对于连接序列很有用,并且能够排除已经找到的国家/地区。
问题是我最终陷入了一个循环,即使要排除的国家似乎处理得当。所以我的想法是,我可能必须以某种方式定义一个基本案例,以便在找到所有可能的国家/地区后停止递归。如何使用 XQuery 实现这一目标?
(:functx.value-union and is-value-in-sequence were found at http://www.xqueryfunctions.com/xq/:)
declare namespace functx = "http://www.functx.com";
declare function functx:value-union
( $arg1 as xs:anyAtomicType* ,
$arg2 as xs:anyAtomicType* ) as xs:anyAtomicType* {
distinct-values(($arg1, $arg2))
};
declare function functx:is-value-in-sequence
( $value as xs:anyAtomicType? ,
$seq as xs:anyAtomicType* ) as xs:boolean {
$value = $seq
} ;
(:Recursive function for finding reachable countries:)
declare function local:findReachable($countries, $country, $reachedCountries) {
let $reachableCountries := $countries[@car_code = $country/border/@country]
for $c in $reachableCountries
where not(functx:is-value-in-sequence($c, $reachedCountries))
return functx:value-union($c, local:findReachable($countries, $c, functx:value-union($reachableCountries,
$reachedCountries)))
};
let $countries := //country
let $startingCountry := //country[@car_code = 'S']
return local:findReachable($countries, $startingCountry, $startingCountry)
最佳答案
你对 $reachedCountries
的检查只能保证国家不会出现两次在同一条路径上,但你仍然沿着每条可能的路径访问每个国家,这需要很长时间时间。没有循环,只有很多冗余。
这是一个简单的深度优先搜索,可以满足您的需求:
declare function local:dfs($stack, $seen) {
if(empty($stack)) then $seen
else (
let $country := $stack[1]
let $neighbors :=
for $code in $country/border/@country[not(. = $seen/@car_code)]
return $country/../country[@car_code = $code]
return local:dfs(($neighbors, $stack[position() > 1]), ($seen, $neighbors))
)
};
local:dfs(doc('mondial.xml')//country[@car_code = 'S'], ())/name
关于xml - 如何处理 XQuery 中的递归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43831040/