使用XQuery获取序列中最重复的元素

deb*_*deb 6 xquery sequence

我有一系列的价值观.他们都可以平等......或不.因此,使用XQuery,我想获得序列中最频繁的项目.

let $counter := 0, $index1 := 0 
for $value in $sequence 
if (count(index-of($value, $sequence))) 
then 
{ 
$counter := count(index-of($value, $sequence)) $index1 := index-of($value) 
} else {} 
Run Code Online (Sandbox Code Playgroud)

我无法做到这一点,所以我想我做错了什么.

在此先感谢你能给我的任何帮助.

Dim*_*hev 6

用途:

  for $maxFreq in 
           max(for $val in distinct-values($sequence)
                     return count(index-of($sequence, $val))
               )
   return
      distinct-values($sequence)[count(index-of($sequence, .)) eq $maxFreq]
Run Code Online (Sandbox Code Playgroud)

2015年12月更新:

这显然更短,但可能效率不高:

$pSeq[index-of($pSeq,.)[max(for $item in $pSeq return count(index-of($pSeq,$item)))]]
Run Code Online (Sandbox Code Playgroud)

可以为XPath 3.1构造最短的表达式:

在此输入图像描述

甚至更短和可复制 - 使用单字符名称:

$s[index-of($s,.)[max($s ! count(index-of($s, .)))]]
Run Code Online (Sandbox Code Playgroud)