title: change flat XML structure to hierarchical structure with XSLT
body: <p>I'm trying to use XSLT to create a hierarchical XML file from a flat XML file, and not sure what the best approach is.</p>
  
  <p>e.g. I need to convert</p>
  
  <pre><code>&lt;root&gt;
  &lt;inventory bag="1" fruit="apple"/&gt;
  &lt;inventory bag="1" fruit="banana"/&gt;
  &lt;inventory bag="2" fruit="apple"/&gt;
  &lt;inventory bag="2" fruit="orange"/&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>&lt;inventory&gt;
  &lt;baglist&gt;
  &lt;bag id="1"/&gt;
  &lt;bag id="2"/&gt;
  &lt;/baglist&gt;
  
  &lt;bag id="1"&gt;
  &lt;fruit id="apple"/&gt;
  &lt;fruit id="banana"/&gt;
  &lt;/bag&gt;
  
  &lt;bag id="2"&gt;
  &lt;fruit id="apple"/&gt;
  &lt;fruit id="orange"/&gt;
  &lt;/bag&gt;
  &lt;/inventory&gt;
  </code></pre>
  
  <p>for N bags/fruits</p>