使用Xquery的元素计数
我需要提取出现次数最多的元素的计数,例如在下面的xml中,我需要将计数返回为2,这是电子邮件元素的计数使用Xquery的元素计数,xquery,Xquery,我需要提取出现次数最多的元素的计数,例如在下面的xml中,我需要将计数返回为2,这是电子邮件元素的计数 <root> <_1> <attributes> <Email> <ov>true</ov> <value> <Email> <ov>tr
<root>
<_1>
<attributes>
<Email>
<ov>true</ov>
<value>
<Email>
<ov>true</ov>
<value>abc@hghh.gff</value>
</Email>
</value>
</Email>
<Email>
<ov>true</ov>
<value>
<Email>
<ov>true</ov>
<value>xyn@qwe.com</value>
</Email>
</value>
</Email>
<UniqueId>
<ov>true</ov>
<value>39919741</value>
</UniqueId>
</attributes>
</_1>
</root>
真的
真的
abc@hghh.gff
真的
真的
xyn@qwe.com
真的
39919741
好的,选择您感兴趣的元素,按节点名称()对它们进行分组,按分组元素的计数进行排序,并按降序排序第一个元素,或按升序排序最后一个元素,例如
for $element in outermost((//Email, //UniqueId))
group by $name := node-name($element)
order by count($element) descending
count $pos
where $pos = 1
return $name || ' : ' || count($element)
在示例输出上运行时电子邮件:2
另一种方法是使用中的例如:highest()
高阶函数,然后将其应用于所选的节点集$E,如下所示:
let $E := (:the selected elements:)
return eg:highest(distinct-values($E!node-name()),
function($n){count($E[node-name() eq $n])})
eg:highest($seq,$f)
函数是一个通用的高阶函数,它查找$seq
中的项对于提供的函数具有最高的值$f
请使用通用方法帮助,因为源XMLI中可能有100个元素。请参阅四个Email
元素,尽管其中两个是嵌套的。有五个ov
元素。因此,你需要更精确地解释你想要达到的或你认为的“最大的发生”。我们可以忽略元素的进一步的子属性,所以我想考虑元素“email”,“UniqueId”,并且基于这些元素,需要对计数进行评估。