title: Embellish XML datasource in XQuery
body: <p>Im using BaseX, which implements XQuery 3.0.</p>
  
  <p>How do I embellish an XML datasource in XQuery, without having to type out all the elements that I want to include? For example, consider the following XML:</p>
  
  <pre><code>&lt;X&gt;
    &lt;name&gt;The root&lt;/name&gt;
    &lt;Y&gt;
       &lt;name&gt; Level 1&lt;/name&gt;
       &lt;Z&gt;
           &lt;name&gt;Level 2a&lt;/name&gt;
           &lt;value&gt;1&lt;/value&gt;
       &lt;/Z&gt;
    &lt;/Y&gt;
    &lt;Y&gt;
       &lt;name&gt;Level 1b&lt;/name&gt;
       &lt;Z&gt;
           &lt;name&gt;Level 2b&lt;/name&gt;
           &lt;value&gt;2&lt;/value&gt;
       &lt;/Z&gt;
    &lt;/Y&gt;
  &lt;/X&gt;
  </code></pre>
  
  <p>I want to add the sum of all values on each level, like this:</p>
  
  <pre><code>&lt;X&gt;
    &lt;name&gt;The root&lt;/name&gt;
    &lt;value&gt;3&lt;/value&gt;
    &lt;Y&gt;
       &lt;name&gt; Level 1&lt;/name&gt;
       &lt;value&gt;1&lt;/value&gt;
       &lt;Z&gt;
           &lt;name&gt;Level 2a&lt;/name&gt;
           &lt;value&gt;1&lt;/value&gt;
       &lt;/Z&gt;
    &lt;/Y&gt;
    &lt;Y&gt;
       &lt;name&gt;Level 1b&lt;/name&gt;
       &lt;value&gt;2&lt;/value&gt;
       &lt;Z&gt;
           &lt;name&gt;Level 2b&lt;/name&gt;
           &lt;value&gt;2&lt;/value&gt;
       &lt;/Z&gt;
    &lt;/Y&gt;
  &lt;/X&gt;
  </code></pre>
  
  <p>I can use an XQuery like this for this:</p>
  
  <pre><code>for $x in /X
  return
  &lt;X&gt;{
    $x/name,
    &lt;value&gt;{sum($x//value)}&lt;/value&gt;,
    for $y in $x/Y
    return
    &lt;Y&gt;{
      $y/name,
      &lt;value&gt;{sum($y//value)}&lt;/value&gt;,
      $y/Z
    }&lt;/Y&gt;
  }&lt;/X&gt;
  </code></pre>
  
  <p>But this gets tedious very fast, when I have a lot of elements that I have to repeat. Is there a way to get this result without having to type out all the attributes and elements I want to preserve in the result set?</p>