title: xPath insert before and after - With DOM and PHP
body: <p><strong>I need to add a class to a HTML structure.</strong></p>
  
  <p>My class is called "<em>container</em>" and should start right after <strong>&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;/h4&gt;</strong> (the child of ul and its simblings, not grandchilds) and should end right before the closing </li> of the same element.</p>
  
  <p><strong>My whole code looks like this:</strong></p>
  
  <pre><code>&lt;?php
  $content = '
      &lt;div class="sidebar-1"&gt;
          &lt;ul&gt;
              &lt;li&gt;
                  &lt;h4&gt;Title&lt;/h4&gt;
                  &lt;ul&gt; 
                      &lt;li&gt;&lt;a href="http://www.test.com"&gt;Test&lt;/a&gt;&lt;/li&gt; 
                      &lt;li&gt;&lt;a href="http://www.test.com"&gt;Test&lt;/a&gt;&lt;/li&gt; 
                  &lt;/ul&gt; 
              &lt;/li&gt; 
              &lt;li&gt;
                  &lt;p&gt;Paragraf&lt;/p&gt;
              &lt;/li&gt; 
              &lt;li&gt;
                  &lt;h4&gt;New title&lt;/h4&gt;
                  &lt;ul&gt; 
                      &lt;li&gt;Some text&lt;/li&gt;
                      &lt;li&gt;Some text åäö&lt;/li&gt;
                  &lt;/ul&gt; 
              &lt;/li&gt; 
          &lt;/ul&gt;
      &lt;/div&gt;
  ';
  
  $doc = new DOMDocument();
  $doc-&gt;loadHTML($content);
  $x = new DOMXPath($doc);
  
  $start_text = '&lt;div class="container"&gt;';
  $end_text = '&lt;/div&gt;';
  
  foreach($x-&gt;query('//div/ul/li') as $anchor)
  {
      $anchor-&gt;insertBefore(new DOMText($start_text),$anchor-&gt;firstChild);
  }
  echo $doc-&gt;saveXML($doc-&gt;getElementsByTagName('ul')-&gt;item(0));
  ?&gt;
  </code></pre>
  
  <p>It works as far as i can add the class opening but not the closing element. I also get strange encoding doing this. I want the output to be the same encoding as the input.</p>
  
  <p><strong>The result should be</strong></p>
  
  <pre><code>    &lt;div class="sidebar-1"&gt;
          &lt;ul&gt;
              &lt;li&gt;
                  &lt;h4&gt;Title&lt;/h4&gt;
                  &lt;div class="content"&gt;
                      &lt;ul&gt; 
                          &lt;li&gt;&lt;a href="http://www.test.com"&gt;Test&lt;/a&gt;&lt;/li&gt; 
                          &lt;li&gt;&lt;a href="http://www.test.com"&gt;Test&lt;/a&gt;&lt;/li&gt; 
                      &lt;/ul&gt;
                  &lt;/div&gt;
              &lt;/li&gt; 
              &lt;li&gt;
                  &lt;div class="content"&gt;
                      &lt;p&gt;Paragraf&lt;/p&gt;
                  &lt;/div&gt;
              &lt;/li&gt; 
              &lt;li&gt;
                  &lt;h4&gt;New title&lt;/h4&gt;
                  &lt;div class="content"&gt;
                      &lt;ul&gt; 
                          &lt;li&gt;Some text&lt;/li&gt;
                          &lt;li&gt;Some text åäö&lt;/li&gt;
                      &lt;/ul&gt; 
                  &lt;/div&gt;
              &lt;/li&gt; 
          &lt;/ul&gt;
      &lt;/div&gt;
  </code></pre>