title: Grouping in XQuery
body: <p>I'm having some trouble grouping the "form" tags. There are 4 types of forms, each form can have more than 1 artwork. Though I've only shown 2 forms in the sample outputs below. As you can see in my output the paintings are <strong>not</strong> grouped together. In the desired output they are. Any help or hints to get my head around it is appreciated.</p>
  
  <p><strong>My Output:</strong></p>
  
  <pre><code>  &lt;author&gt;
      &lt;name&gt;BRAMANTE, Donato&lt;/name&gt;
      &lt;born-died&gt;b. 1444, Fermignano, d. 1514, Roma&lt;/born-died&gt;
      &lt;nationality&gt;Italian&lt;/nationality&gt;
      &lt;biography&gt;Donato Bramante was an Italian architect, who introduced the Early Renaissance style to Milan and the High Renaissance style to Rome, where his most famous design was St. Peter's Basilica.&lt;/biography&gt;
      &lt;artworks form="painting"&gt;
        &lt;artwork date="1477"&gt;
          &lt;form&gt;painting&lt;/form&gt;
          &lt;artworkForm&gt;painting&lt;/artworkForm&gt;
          &lt;title&gt;Heraclitus and Democritus&lt;/title&gt;
          &lt;technique&gt;Fresco transferred to canvas&lt;/technique&gt;
          &lt;location&gt;Pinacoteca di Brera, Milan&lt;/location&gt;
        &lt;/artwork&gt;
      &lt;/artworks&gt;
      &lt;artworks form="painting"&gt;
        &lt;artwork date="1477"&gt;
          &lt;form&gt;painting&lt;/form&gt;
          &lt;artworkForm&gt;painting&lt;/artworkForm&gt;
          &lt;title&gt;Heraclitus and Democritus&lt;/title&gt;
          &lt;technique&gt;Fresco transferred to canvas&lt;/technique&gt;
          &lt;location&gt;Pinacoteca di Brera, Milan&lt;/location&gt;
        &lt;/artwork&gt;
      &lt;/artworks&gt;
      &lt;artworks form="architecture"&gt;
        &lt;artwork date="1485"&gt;
          &lt;form&gt;architecture&lt;/form&gt;
          &lt;artworkForm&gt;architecture&lt;/artworkForm&gt;
          &lt;title&gt;Interior view toward choir&lt;/title&gt;
          &lt;technique&gt;Fresco, height of arch 10,6 m&lt;/technique&gt;
          &lt;location&gt;Santa Maria presso San Satiro, Milan&lt;/location&gt;
        &lt;/artwork&gt;
      &lt;/artworks&gt;
    &lt;/author&gt;
  </code></pre>
  
  <p><strong>Desired Output</strong></p>
  
  <pre><code>&lt;author&gt;
      &lt;name&gt;BRAMANTE, Donato&lt;/name&gt;
      &lt;born-died&gt;b. 1444, Fermignano, d. 1514, Roma&lt;/born-died&gt;
      &lt;nationality&gt;Italian&lt;/nationality&gt;
      &lt;biography&gt;Donato Bramante was an Italian architect, who introduced the Early Renaissance style to Milan and the High Renaissance style to Rome, where his most famous design was St. Peter's Basilica.&lt;/biography&gt;
      &lt;artworks form="painting"&gt;
        &lt;artwork date="1477"&gt;
          &lt;form&gt;painting&lt;/form&gt;
          &lt;artworkForm&gt;painting&lt;/artworkForm&gt;
          &lt;title&gt;Heraclitus and Democritus&lt;/title&gt;
          &lt;technique&gt;Fresco transferred to canvas&lt;/technique&gt;
          &lt;location&gt;Pinacoteca di Brera, Milan&lt;/location&gt;
        &lt;/artwork&gt; 
        &lt;artwork date="1477"&gt;
          &lt;form&gt;painting&lt;/form&gt;
          &lt;artworkForm&gt;painting&lt;/artworkForm&gt;
          &lt;title&gt;Heraclitus and Democritus&lt;/title&gt;
          &lt;technique&gt;Fresco transferred to canvas&lt;/technique&gt;
          &lt;location&gt;Pinacoteca di Brera, Milan&lt;/location&gt;
        &lt;/artwork&gt;
      &lt;/artworks&gt;
      &lt;artworks form="architecture"&gt;
        &lt;artwork date="1485"&gt;
          &lt;form&gt;architecture&lt;/form&gt;
          &lt;artworkForm&gt;architecture&lt;/artworkForm&gt;
          &lt;title&gt;Interior view toward choir&lt;/title&gt;
          &lt;technique&gt;Fresco, height of arch 10,6 m&lt;/technique&gt;
          &lt;location&gt;Santa Maria presso San Satiro, Milan&lt;/location&gt;
        &lt;/artwork&gt;
      &lt;/artworks&gt;
    &lt;/author&gt;
  </code></pre>
  
  <p><strong>My XQuery:</strong></p>
  
  <pre><code>&lt;authors&gt;
      {
      for $author in doc("authors.xml")/authors/author
      let $artworks := doc("artworks.xml")/artworks/artwork
          return 
              &lt;author&gt;
                  &lt;name&gt;{$author/name/text()}&lt;/name&gt;
                  &lt;born-died&gt;{$author/born-died/text()}&lt;/born-died&gt;
                  &lt;nationality&gt;{$author/nationality/text()}&lt;/nationality&gt;
                  &lt;biography&gt;{$author/biography/text()}&lt;/biography&gt;
                  {
                      for $artwork in $artworks
                      let $nameInAuthor := $author/name/text()
                      let $nameInArtwork := $artwork/author/text()
                      where $nameInAuthor = $nameInArtwork
                          return 
                              &lt;artworks form="{$artwork/form/text()}"&gt;
                                  &lt;artwork date="{$artwork/date/text()}"&gt;
                                      &lt;title&gt;{$artwork/title/text()}&lt;/title&gt;
                                      &lt;technique&gt;{$artwork/technique/text()}&lt;/technique&gt;
                                      &lt;location&gt;{$artwork/location/text()}&lt;/location&gt;
                                  &lt;/artwork&gt;
                              &lt;/artworks&gt;
                  }
              &lt;/author&gt;
      }
  &lt;/authors&gt;
  </code></pre>
  
  <p>I've tried using distinct-values() but i just get the same output as my previous output above</p>
  
  <pre><code>&lt;authors&gt;
      {
      for $author in doc("authors.xml")/authors/author
      let $artworks := doc("artworks.xml")/artworks/artwork
          return 
              &lt;author&gt;
                  &lt;name&gt;{$author/name/text()}&lt;/name&gt;
                  &lt;born-died&gt;{$author/born-died/text()}&lt;/born-died&gt;
                  &lt;nationality&gt;{$author/nationality/text()}&lt;/nationality&gt;
                  &lt;biography&gt;{$author/biography/text()}&lt;/biography&gt;
                  {
                      for $artwork in $artworks
                      let $nameInAuthor := $author/name/text()
                      let $nameInArtwork := $artwork/author/text()
                      where $nameInAuthor = $nameInArtwork
                          return 
                              for $artworkForm in distinct-values($artworks/form/text())
                              let $form := $artwork/form/text()
                              where $form = $artworkForm
                                  return
                                      &lt;artworks form="{$artworkForm}"&gt;
                                      {           
                                          &lt;artwork date="{$artwork/date/text()}"&gt;
                                              &lt;form&gt;{$form}&lt;/form&gt;
                                              &lt;artworkForm&gt;{$artworkForm}&lt;/artworkForm&gt;
                                              &lt;title&gt;{$artwork/title/text()}&lt;/title&gt;
                                              &lt;technique&gt;{$artwork/technique/text()}&lt;/technique&gt;
                                              &lt;location&gt;{$artwork/location/text()}&lt;/location&gt;
                                          &lt;/artwork&gt;
                                      }
                                      &lt;/artworks&gt;
  
                  }
              &lt;/author&gt;
      }
  &lt;/authors&gt;
  </code></pre>