title: XQuery Nested For Loop
body: <p>I've a sample xml file such as : </p>
  
  <pre><code>&lt;user_manual document-type="IU" ts-cover="Language Part alt" ts-lang="-" ts-multi="Language Part, Chapter" ts-overview="-" metadata1="9211" brand="Bosch" material-number="dummy_9000727237" vib="RW404960" language-codes="de, en" production-date="2012-12-03" layout-type="-" id="SL9761901"&gt;
  &lt;embed-language_part id="SL14686180"&gt;
  &lt;language_part id="ecls_bio_becls_a3_a14686169.SL9761756.7" role="-" lang="de"&gt;
  &lt;embed-user_manual_part id="ecls_bio_becls_a3_a14686169.SL14686446.10"&gt;
  &lt;?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a17144756"?&gt; 
  &lt;user_manual_part id="ecls_bio_becls_a3_a17144756.SL9765760.11" role="-" document-type="IU"&gt;
  &lt;embed-chapter id="ecls_bio_becls_a3_a17144756.SL9762101.13"&gt;
  &lt;?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a30660983"?&gt; 
  &lt;chapter id="ecls_bio_becls_a3_a30660983.SL2300626.14" role="-" toctitle="yes" footrowtitle="no" type="security"&gt;
  &lt;embed-title_module id="ecls_bio_becls_a3_a30660983.SL2361816.15"&gt;
  &lt;?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a611713"?&gt; 
  &lt;title_module id="ecls_bio_becls_a3_a611713.SL873735.16" role="-"&gt;
  &lt;title id="ecls_bio_becls_a3_a611713.SL873736.17"&gt;Sicherheits- und Warnhinweise&lt;/title&gt; 
  &lt;/title_module&gt;
  &lt;?ecls-end-embedded-resource resource="ecls_bio_becls_a3_a611713"?&gt; 
  &lt;/embed-title_module&gt;
  &lt;embed-section id="ecls_bio_becls_a3_a30660983.SL10400094.18"&gt;
  &lt;?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a11752692"?&gt;  
  &lt;section id="ecls_bio_becls_a3_a11752692.SL1742298.19" footrowtitle="no" role="-" toctitle="yes"&gt;
  &lt;?Pub Caret1?&gt; 
  &lt;embed-title_module id="ecls_bio_becls_a3_a11752692.SL1742291.20"&gt;
  &lt;?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a16181733"?&gt; 
  &lt;title_module id="ecls_bio_becls_a3_a16181733.SL984238.21" role="-"&gt;
  &lt;title id="ecls_bio_becls_a3_a16181733.SL984239.22"&gt;Bevor Sie das Gerat in Betrieb nehmen&lt;/title&gt; 
  &lt;para id="ecls_bio_becls_a3_a16181733.SL984240.23"&gt;Lesen Sie Gebrauchs- und Montageanleitung aufmerksam durch! Sie enthalten wichtige Informationen �ber Aufstellen, Gebrauch und Wartung des Ger�tes.&lt;/para&gt; 
  &lt;para id="ecls_bio_becls_a3_a16181733.SL984241.24"&gt;Der Hersteller haftet nicht, wenn Sie die Hinweise und Warnungen der Gebrauchsanleitung missachten. Bewahren Sie alle Unterlagen f�r sp�teren Gebrauch oder f�r Nachbesitzer auf.&lt;/para&gt; 
  &lt;/title_module&gt;
  &lt;?ecls-end-embedded-resource resource="ecls_bio_becls_a3_a16181733"?&gt; 
  &lt;/embed-title_module&gt;
  &lt;/section&gt;
  &lt;?Pub *0000000275?&gt; 
  &lt;?ecls-end-embedded-resource resource="ecls_bio_becls_a3_a11752692"?&gt; 
  &lt;/embed-section&gt;
  &lt;/chapter&gt;
  &lt;/embed-chapter&gt;
  &lt;/user_manual_part&gt;
  &lt;/embed-user_manual_part&gt;
  &lt;/language_part&gt;
  &lt;/embed-language_part&gt;
  &lt;/user_manual&gt;
  </code></pre>
  
  <p>I want to use the XQuery language but i am really new to this query language.</p>
  
  <p>The infrastructure I need is this : I want to get the chapter's and its' sections with their titles, for example:</p>
  
  <p>The chapter is like in this xml file :</p>
  
  <pre><code>    &lt;chapter id="ecls_bio_becls_a3_a30660983.SL2300626.14" role="-" toctitle="yes" footrowtitle="no" type="security"&gt;
      &lt;embed-title_module id="ecls_bio_becls_a3_a30660983.SL2361816.15"&gt;
      &lt;?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a611713"?&gt; 
      &lt;title_module id="ecls_bio_becls_a3_a611713.SL873735.16" role="-"&gt;
      &lt;title id="ecls_bio_becls_a3_a611713.SL873736.17"&gt;Sicherheits- und Warnhinweise&lt;/title&gt;
  ...
  </code></pre>
  
  <p>In that example Sicherheits- ind Warnhinweise is the title of chapter element. Chapters can have many sections and in our example section's title is :</p>
  
  <pre><code>       &lt;section id="ecls_bio_becls_a3_a11752692.SL1742298.19" footrowtitle="no" role="-" toctitle="yes"&gt;
          &lt;?Pub Caret1?&gt; 
          &lt;embed-title_module id="ecls_bio_becls_a3_a11752692.SL1742291.20"&gt;
          &lt;?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a16181733"?&gt; 
          &lt;title_module id="ecls_bio_becls_a3_a16181733.SL984238.21" role="-"&gt;
          &lt;title id="ecls_bio_becls_a3_a16181733.SL984239.22"&gt;Bevor Sie das Gerat in Betrieb nehmen&lt;/title&gt; 
  ...
  </code></pre>
  
  <p>Bevor Sie das Gerat in Betrieb nehmen.</p>
  
  <p>The expected structure :</p>
  
  <pre><code>&lt;chapter&gt; title:"Chapter's title"
  &lt;section&gt;title:"First section's title"&lt;/section&gt;
  &lt;section&gt;title:"Second section's title"&lt;/section&gt;
  &lt;/chapter&gt;
  </code></pre>
  
  <p>For that example chapter has just one section but I just configured this one to reach a solution of my file, it is most readable configuration I guess...</p>
  
  <p>I tried below xquery query :</p>
  
  <pre><code>&lt;chapter
      let $doc := doc("Test.xml")
      for $chapter in $doc/user_manual/embed-language_part/language_part/embed-user_manual_part/user_manual_part/embed-chapter/chapter
          let $chapter_title := $chapter/embed-title_module/title_module/title
          for $section in $chapter/embed-section/section
          return &lt;chapter&gt;title:{data($chapter_title)} &lt;section&gt; title:{data($section/embed-title_module/title_module/title)}&lt;/section&gt;&lt;/chapter&gt;
  </code></pre>
  
  <p>with two nested loops, but this code makes my structure like :</p>
  
  <pre><code>&lt;chapter&gt;title:"Chapter's title"&lt;section&gt;"First section's title"&lt;/section&gt;&lt;/chapter&gt;
  &lt;chapter&gt;title:"Chapter's title"&lt;section&gt;"Second section's title"&lt;/section&gt;&lt;/chapter&gt;
  </code></pre>
  
  <p>The problem was looping but when I try to edit code like this :</p>
  
  <pre><code>let $doc := doc("assemble.xml")
      for $chapter in $doc/user_manual/embed-language_part/language_part/embed-user_manual_part/user_manual_part/embed-chapter/chapter
          let $chapter_title := $chapter/embed-title_module/title_module/title
          &lt;chapter&gt; title:{data($chapter_title)} {
          for $section in $chapter/embed-section/section
          return &lt;section&gt; title:{data($section/embed-title_module/title_module/title)}&lt;/section&gt;
          }&lt;/chapter&gt;
  </code></pre>
  
  <p>I just want to put the  tag in the first loop ones, after that i will add section elements to query. But the above command is not working for my expectation.</p>
  
  <p>So i need your suggestions to get these structure properly. Thank you in advance. I wish i was clearly told the situation to all of you. The name of file assumed as "assemble.xml".</p>