title: XSLT - Dynamic nesting of elements
body: <p>I'm working on a stylesheet that outputs HTML from XML inputs. I have to generate different nesting levels in the output file according to the position of a list of elements in the input. For instance, NEWS[1] and NEWS[2] should be nested under same element as well as NEWS[3] and NEWS[4].</p>
  
  <p>This is an example of XML input:</p>
  
  <pre><code>&lt;NEWS_LIST&gt;
      &lt;NEWS&gt;
          &lt;TITLE&gt;Title of the notice #1&lt;/TILE&gt;
          &lt;IMG_URL&gt;http://media/image_1.png&lt;/IMG_URL&gt;
      &lt;/NEWS&gt;
      &lt;NEWS&gt;
          &lt;TITLE&gt;Title of the notice #2&lt;/TILE&gt;
          &lt;IMG_URL&gt;http://media/image_2.png&lt;/IMG_URL&gt;
      &lt;/NEWS&gt;
      &lt;NEWS&gt;
          &lt;TITLE&gt;Title of the notice #3&lt;/TILE&gt;
          &lt;IMG_URL&gt;http://media/image_3.png&lt;/IMG_URL&gt;
      &lt;/NEWS&gt;
      &lt;NEWS&gt;
          &lt;TITLE&gt;Title of the notice #4&lt;/TILE&gt;
          &lt;IMG_URL&gt;http://media/image_4.png&lt;/IMG_URL&gt;
      &lt;/NEWS&gt;
  &lt;/NEWS_LIST&gt;
  </code></pre>
  
  <p>Desired HTML output:</p>
  
  <pre><code>&lt;div class="middle&gt;
      &lt;div class="unit 1"&gt;
          &lt;div class="unit 2"&gt;
              &lt;img src="http://media/image_1.png"/&gt;
              &lt;p&gt;Title of the notice #1&lt;/p&gt;
          &lt;/div&gt;
          &lt;div class="unit 2"&gt;
              &lt;img src="http://media/image_2.png"/&gt;
              &lt;p&gt;Title of the notice #2&lt;/p&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="unit 1"&gt;
          &lt;div class="unit 2"&gt;
              &lt;img src="http://media/image_3.png"/&gt;
              &lt;p&gt;Title of the notice #3&lt;/p&gt;
          &lt;/div&gt;
          &lt;/div class="unit 2"&gt;
              &lt;img src="http://media/image_4.png"/&gt;
              &lt;p&gt;Title of the notice #4&lt;/p&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I am using XSLT position() function to select elements and  to output files but I don't know how to nest childs. Any help would be really appreciated.</p>