title: Uncaught exception 'DOMException' with message 'Not Found Error'
body: <p>Bascially I'm writing a templating system for my CMS and I want to have a modular structure which involves people putting in tags like:</p>
  
  <p><code>&lt;module name="news" /&gt;</code> or <code>&lt;include name="anotherTemplateFile" /&gt;</code> which I then want to find in my php and replace with dynamic html.</p>
  
  <p>Someone on here pointed me towards DOMDocument, but I've already come across a problem.</p>
  
  <p>I'm trying to find all <code>&lt;include /&gt;</code> tags in my template and replace them with some simple html. Here is my template code:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;title&gt;CMS&lt;/title&gt;
  &lt;include name="head" /&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;include name="header" /&gt;
  
  &lt;include name="content" /&gt;
  
  &lt;include name="footer" /&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is my PHP:</p>
  
  <pre><code>$template = new DOMDocument();
  $template-&gt;load("template/template.tpl");
  
  foreach( $template-&gt;getElementsByTagName("include") as $include ) {
      $element = '&lt;input type="text" value="'.print_r($include, true).'" /&gt;';
      $output = $template-&gt;createTextNode($element);
      $template-&gt;replaceChild($output, $include);
  }
  
  echo $template-&gt;saveHTML();
  </code></pre>
  
  <p>Now, I get the fatal error <code>Uncaught exception 'DOMException' with message 'Not Found Error'</code>.</p>
  
  <p>I've looked this up and it seems to be that because my <code>&lt;include /&gt;</code> tags aren't necessarily DIRECT children of <code>$template</code> its not replacing them.</p>
  
  <p>How can I replace them independently of descent?</p>
  
  <p>Thank you</p>
  
  <p>Tom</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Basically I had a brainwave of sorts. If I do something like this for my PHP I see its trying to do what I want it to do:</p>
  
  <pre><code>$template = new DOMDocument();
      $template-&gt;load("template/template.tpl");
  
      foreach( $template-&gt;getElementsByTagName("include") as $include ) {
          $element = '&lt;input type="text" value="'.print_r($include, true).'" /&gt;';
          $output = $template-&gt;createTextNode($element);
          // this line is different:
          $include-&gt;parentNode-&gt;replaceChild($output, $include);
      }
  
      echo $template-&gt;saveHTML();
  </code></pre>
  
  <p>However it only seems to change 1 occurence in the <code>&lt;body&gt;</code> of my HTML... when I have 3. :/</p>