title: PHP DomDocument removing an element scrambles HTML
body: <p>I am having issues removing a node using PHP DomDocument.</p>
  
  <p>I have some HTML like so:</p>
  
  <pre><code>&lt;!DOCTYPE HTML "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;
  &lt;html&gt;
  &lt;head&gt; 
  &lt;title&gt;Test&lt;/title&gt;
  &lt;script id="fr21" type="text/javascript" src="jquery.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt; 
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I attempt to remove the script node like so:</p>
  
  <pre><code>$jquery_node = $doc-&gt;getElementById('fr21'); 
  
  $head_node = $jquery_node-&gt;parentNode;
  
  $head_node-&gt;removeChild($jquery_node); 
  </code></pre>
  
  <p>I then try to view the HTML by echo:</p>
  
  <pre><code>echo $doc-&gt;saveHTML().'&lt;br&gt;&lt;br&gt;';
  </code></pre>
  
  <p>The HTML then becomes this:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;body&gt;&lt;p&gt;-//W3C//DTD HTML 4.0 Transitional//EN"&amp;gt;&lt;/p&gt; 
  &lt;body&gt; 
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>What just happened? The HTML has been mangled? Am I not removing the node correctly?</p>
  
  <p>The weird thing is when I calculate the xPath for the jquery node it is shown as if its attached to the body node rather than the head node?</p>
  
  <pre><code>/html[1]/body[1]/script[1]
  </code></pre>