你正在从一个迫切需要的角度来解决这个问题。
在XQuery中,您可以设置变量的值,但您永远无法更改它们。
做迭代式算法的正确方法是用递归函数:
declare funciton local:most($sequence, $index, $value, $count)
{
let $current=$sequence[$index]
return
if (empty($current))
then $value
else
let $current-count = count(index-of($current, $sequence))
return
if ($current-count > $count)
then local:most($sequence, $index+1, $current, $current-count)
else local:most($sequence, $index+1, $value, $count)
}
但接近问题的一个更好的方法是通过描述一个非迭代的方式的问题。在这种情况下,序列中的所有不同值都需要显示任何不同值的最大次数的值。
以前森泰斯翻译成XQuery是
let $max-count := max(for $value1 in distinct-values($sequence)
return count(index-of($sequence, $value1)))
for $value2 in distinct-values($sequence)
where (count(index-of($sequence, $value2)) = $max-count
return $value2
再好问题(+1)。答案是一个XPath单行表达式......将尽量缩短它。 – 2010-06-24 19:17:12