0
votes

I need to extract the count of that element which has the maximum occurrences, For example in below xml, I need count to be returned as 2 which is for the Email element.

<root>
   <_1>
      <attributes>
         <Email>
            <ov>true</ov>
            <value>
               <Email>
                  <ov>true</ov>
                 <value>[email protected]</value>
               </Email>
            </value>
         </Email>
         <Email>
            <ov>true</ov>
            <value>
               <Email>
                  <ov>true</ov>
                  <value>[email protected]</value>
               </Email>
            </value>
         </Email>
         <UniqueId>
            <ov>true</ov>
            <value>39919741</value>
         </UniqueId>
      </attributes>
   </_1>
</root>
2
Please help with a generic approach, as there can be 100 of element in source XML - Anjit Sharma
I see four Email elements,although two of them are nested. There are five ov elements. So you will need to explain more precisely what you want to achieve or what you consider a "maximum occurrence". - Martin Honnen
We can ignore further child attributes of the element, so I want to consider elements "Email" ,"UniqueId " and based on them, need to evaluate the count - Anjit Sharma

2 Answers

1
votes

Well, select the elements you are interested in, group them by the node-name(), order by the count of the grouped elements and take the first in descending sort order or last in ascending sort order e.g.

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)

when run on your sample outputs Email : 2.

Example at https://xqueryfiddle.liberty-development.net/6qM2e2c

0
votes

Another approach is to use the eg:highest() higher order function found in https://www.w3.org/TR/xpath-functions-31/#highest-lowest, and then apply it to the selected set of nodes $E as follows:

let $E := (:the selected elements:)
return eg:highest(distinct-values($E!node-name()), 
                  function($n){count($E[node-name() eq $n])})

The eg:highest($seq, $f) function is a generic higher-order function that finds the items in $seq having the highest value for the supplied function $f.