title: Nest all un-nested XML elements into a new element using XSLT
body: <p>I have an XML file that needs some corrections in its structure. Because of the size of said document this has to be done automatically.<br>
  The basic structure is the following:</p>
  
  <pre><code>&lt;body&gt;
      &lt;div author='author1'&gt;
          &lt;lb n='1'/&gt;Text1
          &lt;lb n='2'/&gt;Text2
      &lt;/div&gt;
      &lt;lb n='3'/&gt;Text3
      &lt;lb n='4'/&gt;Text4
      &lt;div author='author1'&gt;
          &lt;lb n='5'/&gt;Text5
      &lt;/div&gt;
      &lt;add&gt;xyz&lt;/add&gt;
      &lt;lb n='6'/&gt;Text6
      &lt;lb n='7'/&gt;Text7
      &lt;lb n='8'/&gt;Text8
  &lt;/body&gt;
  </code></pre>
  
  <p>What I want to do is to put everything that isn't already in <code>div</code> tags into <code>div</code> tags and mark it with the attribute <code>@author='author2'</code>. The text and structure of the moved chunks should stay as it is, just nested into a <code>div</code>. The order has to be conserved as well.<br>
  The resulting XML should look something like this:</p>
  
  <pre><code>&lt;body&gt;
      &lt;div author='author1'&gt;
          &lt;lb n='1'/&gt;Text1
          &lt;lb n='2'/&gt;Text2
      &lt;/div&gt;
      &lt;div author='author2'&gt;
          &lt;lb n='3'/&gt;Text3
          &lt;lb n='4'/&gt;Text4
      &lt;/div&gt;
      &lt;div author='author1'&gt;
          &lt;lb n='5'/&gt;Text5
      &lt;/div&gt;
      &lt;div author='author2'&gt;
          &lt;add&gt;xyz&lt;/add&gt;
          &lt;lb n='6'/&gt;Text6
          &lt;lb n='7'/&gt;Text7
          &lt;lb n='8'/&gt;Text8
      &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>My current XSLT (which I wrote before noticing that the XML has more elements than just <code>lb</code>) does put <code>lb</code> in the correct <code>div</code>, but it moves everything to the bottom and deletes the text.<br>
  The XSLT looks like this:</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="2.0"&gt;
  
  &lt;xsl:template match="@* | node()"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@* | node()"/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
  &lt;xsl:template match="body"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="*[not(self::lb)]"/&gt;
              &lt;div&gt;
                  &lt;xsl:attribute name="resp"&gt;author2&lt;/xsl:attribute&gt;
                  &lt;xsl:apply-templates select="lb"/&gt;
              &lt;/div&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>And it returns this XML:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;body&gt;&lt;div author="author1"&gt;
          &lt;lb n="1"/&gt;Text1
          &lt;lb n="2"/&gt;Text2
      &lt;/div&gt;&lt;div author="author1"&gt;
          &lt;lb n="5"/&gt;Text5
      &lt;/div&gt;&lt;add&gt;xyz&lt;/add&gt;&lt;div resp="author2"&gt;&lt;lb n="3"/&gt;&lt;lb n="4"/&gt;&lt;lb n="6"/&gt;&lt;lb n="7"/&gt;&lt;lb n="8"/&gt;&lt;/div&gt;&lt;/body&gt;
  </code></pre>
  
  <p>What am I doing wrong?<br>
  Thanks for your help in advance!</p>