4
我试图通过使用mondial.sql数据库通过陆地边界从一个国家穿越到另一个国家,找到所有可通过陆地到达的国家。它必须以递归方式完成,并且我发现了一些在线的函数,我认为这些函数对于加入序列是有用的,并且能够排除已经找到的国家。如何处理XQuery中的递归?
问题是我最终在一个循环中,即使要排除的国家似乎得到妥善处理。所以我的想法是,我可能不得不以某种方式定义一个基本案例,以便在找到所有可能的国家后停止递归。如何用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)