XQuery - group by and count

后端 未结 1 957
一向
一向 2021-01-05 10:59

This is the structure of my XML file -


  James Bond
  Allen Bond
  

        
相关标签:
1条回答
  • 2021-01-05 11:12

    XQuery 3.0 solution with group by:

    <A>{
      for $name in //Name
      let $full :=
        if(not($name/@p)) then concat($name, ' ', $name/@t)
        else concat($name, ' ', $name/@t, ', ', $name/@p)
      group by $full
      return <N>{$full, '-', count($name)}</N>
    }</A>
    

    XQuery 1.0 solution with distinct-values($arg):

    <A>{
      let $names :=
        for $name in //Name
        return if(not($name/@p)) then concat($name, ' ', $name/@t)
        else concat($name, ' ', $name/@t, ', ', $name/@p)
      for $name in distinct-values($names)
      return <N>{$name, '-', count($names[. eq $name])}</N>
    }</A>
    
    0 讨论(0)
提交回复
热议问题