Grouping in XQuery : group « XQuery « XML






Grouping in XQuery


File: Data.xml

<!-- car order docuCt -->
<order>
  <car dept="A">
    <number>1</number>
    <name>name 1</name>
  </car>
  <car dept="B">
    <number>2</number>
    <name>name 2</name>
  </car>
  <car dept="B">
    <number>3</number>
    <name>name 3</name>
  </car>
  <car dept="C">
    <number>4</number>
    <name>name 4</name>
  </car>
</order>


File: Query.xquery

for $d in distinct-values(doc("Data.xml")//car/@dept)
return 
  <result dept="{$d}" 
          count="{count(doc("Data.xml")//car[@dept = $d])}"/>



Output:

<?xml version="1.0" encoding="UTF-8"?>
<result count="1" dept="A"/>
<result count="2" dept="B"/>
<result count="1" dept="C"/>

 








Related examples in the same category

1.Grouping by department