我正在尝试将 XQuery/XPath 与mondial 数据库的 XML 版本一起使用,以查找彼此相距最远的两个城市,仅考虑人口超过 500,000 的城市。为了计算距离,我假设一个墨卡托投影。
这是我的代码:
let $db := doc("mondial.xml")
(: Cities with 500,000+ population :)
let $cities := (for $city in $db/mondial/country//city
(: Compare using latest population data :)
where $city/population[@year = max($city/population/@year)] > 500000
return $city)
(: City pairs and the distances between them (not
not square-rooted as all we care about is order) :)
let $distancesSquared := (for $city1 in $cities
for $city2 in $cities
where $city1 != $city2
return <distancesquared city1="{ data($city1/name) }" city2="{ data($city2/name) }">{ (
let $diffLong := number($city1/longitude) - number($city2/longitude)
let $diffLat := number($city1/latitude) - number($city2/latitude)
return $diffLong * $diffLong + $diffLat * $diffLat
) }</distancesquared>)
let $max := max($distancesSquared)
return $distancesSquared[data(.) = $max]
但由于某种原因,我在这一行遇到了分段错误:
let $max := max($distancesSquared)
我xqilla
用来运行这样的查询:
xqilla -o query.xml query.xq
任何想法我的代码可能有什么问题?