我有这段代码
for $sea in distinct-values (doc("mondial.xml")//island/tokenize(@sea,"\s"))
let $count := count(doc("mondial.xml")//island[@sea = $sea]/tokenize(@sea,"\s"))
let $seacount := concat($sea," ",$count)
return $seacount
返回给我
“sea-ArcticOcean 3 sea-Greenlandsea 0 sea-NorwegianSea 1 sea-BarentsSea 0
sea-Atlantic 53 sea-LabradorSea 0 ...“等等。
数字是你可以看到$ count和sea-XXX部分是变量$ sea。我现在坚持的是以某种方式选择$ sea,它具有所有计数的最大值$ count并显示该海名。我尝试过将$ max变量添加到:
for $sea in distinct-values (doc("mondial.xml")//island/tokenize(@sea,"\s"))
let $count := count(doc("mondial.xml")//island[@sea = $sea]/tokenize(@sea,"\s"))
let $seacount := concat($sea," ",$count)
$max := concat($sea," ",max($count))
return $max
还尝试通过$ count(descending)语句添加订单,然后返回第一个节点:
for $sea in distinct-values (doc("mondial.xml")//island/tokenize(@sea,"\s"))
let $count := count(doc("mondial.xml")//island[@sea = $sea]/tokenize(@sea,"\s"))
let $seacount := concat($sea," ",$count)
order by $count descending
return $seacount[1]
我是以错误的方式接近这个还是?
答案 0 :(得分:0)
这将返回每个迭代的结果序列的第一个节点。在这种情况下,$seacount
始终是1个字符串的序列,因此您始终选择等效的('sea-value count')[1]
,这只是单个字符串值。要仅选择一个整体,将整个查询包装在parens中以创建所有迭代的所有值的序列,然后选择第一个:
(for $sea in distinct-values (doc("mondial.xml")//island/tokenize(@sea,"\s"))
let $count := count(doc("mondial.xml")//island[@sea = $sea]/tokenize(@sea,"\s"))
let $seacount := concat($sea," ",$count)
order by $count descending
return $seacount)[1]