title: How to properly generate an xml from xquery
body: <p>I'm new to xquery and is trying to read different references on using the tool. I've been trying to play around testing and trying to generate some xml format messages but this one puzzles me. Here's my xQuery code:</p>
  
  <p><strong>Sample XQuery</strong></p>
  
  <pre><code>declare variable $requestBody as element() external;
  declare function VerifyOrderDetailTransformation($requestBody as element())
  as element() {
      &lt;msg&gt;
          &lt;header&gt;
              &lt;headtitle&gt;This is the title&lt;/headtitle&gt;
          &lt;/header&gt;
          &lt;dbody&gt;
              {GenerateEquipmentListNodes($requestBody)} 
          &lt;/dbody&gt;
      &lt;/msg&gt;
  };
  
  declare function GenerateEquipmentListNodes($requestBody as element())
  as element()* {
  
      let $titleList := (
          for $e in $requestBody//bookstore//book
              let $dTitle := $e/title/text()
              return 
                 &lt;theTitle&gt;{$dTitle}&lt;/theTitle&gt;
          )
  
      return 
         &lt;dTitleList&gt;
             {$titleList}
         &lt;/dTitleList&gt;
  };
  
  VerifyOrderDetailTransformation($requestBody)
  </code></pre>
  
  <p><strong>Sample XML</strong></p>
  
  <pre><code>&lt;bookstore&gt;
  
  &lt;book category="COOKING"&gt;
      &lt;title lang="en"&gt;Everyday Italian&lt;/title&gt;
      &lt;author&gt;Giada De Laurentiis&lt;/author&gt;
      &lt;year&gt;2005&lt;/year&gt;
      &lt;price&gt;30.00&lt;/price&gt;
  &lt;/book&gt;
  
  &lt;book category="CHILDREN"&gt;
      &lt;title lang="en"&gt;Harry Potter&lt;/title&gt;
      &lt;author&gt;J K. Rowling&lt;/author&gt;
      &lt;year&gt;2005&lt;/year&gt;
      &lt;price&gt;29.99&lt;/price&gt;
  &lt;/book&gt;
  
  &lt;book category="WEB"&gt;
      &lt;title lang="en"&gt;XQuery Kick Start&lt;/title&gt;
      &lt;author&gt;James McGovern&lt;/author&gt;
      &lt;author&gt;Per Bothner&lt;/author&gt;
      &lt;author&gt;Kurt Cagle&lt;/author&gt;
      &lt;author&gt;James Linn&lt;/author&gt;
      &lt;author&gt;Vaidyanathan Nagarajan&lt;/author&gt;
      &lt;year&gt;2003&lt;/year&gt;
      &lt;price&gt;49.99&lt;/price&gt;
  &lt;/book&gt;
  
  &lt;book category="WEB"&gt;
      &lt;title lang="en"&gt;Learning XML&lt;/title&gt;
      &lt;author&gt;Erik T. Ray&lt;/author&gt;
      &lt;year&gt;2003&lt;/year&gt;
      &lt;price&gt;39.95&lt;/price&gt;
  &lt;/book&gt;
  
  &lt;/bookstore&gt;
  </code></pre>
  
  <p>And here's the output generated by running xQuery on the XML:</p>
  
  <p><strong>Current output</strong></p>
  
  <pre><code>&lt;msg&gt; 
      &lt;head&gt; 
          &lt;title&gt;This is the title&lt;/title&gt; 
      &lt;/head&gt; 
      &lt;body&gt; 
          &lt;dTitleList/&gt; 
      &lt;/body&gt; 
  &lt;/msg&gt;
  </code></pre>
  
  <p><strong>Expected Output</strong></p>
  
  <pre><code>&lt;msg&gt; 
      &lt;head&gt; 
          &lt;title&gt;This is the title&lt;/title&gt; 
      &lt;/head&gt; 
      &lt;body&gt; 
          &lt;dTitleList&gt; 
          &lt;theTitle&gt;Everyday Italian&lt;/theTitle&gt;
          &lt;theTitle&gt;Harry Potter&lt;/theTitle&gt;
          &lt;theTitle&gt;XQuery Kick Start&lt;/theTitle&gt;
          &lt;theTitle&gt;Learning XML&lt;/theTitle&gt;
          &lt;dTitleList/&gt; 
      &lt;/body&gt; 
  &lt;/msg&gt;
  </code></pre>
  
  <hr>
  
  <p>My question is, what could I have probably missed?</p>