title: How can this DOM translation or merging XML/HTML be done in PHP?
body: <p>I have a XML file and a HTML file and would like to combine this to a new document. </p>
  
  <p>For me this is the first time to do things like this as well that I'm not a PHP developer.</p>
  
  <p>The result of this should become a word document...</p>
  
  <h2>XML file</h2>
  
  <p>This file will be generated (source: drupal)</p>
  
  <pre><code>&lt;professie&gt;Manager&lt;/professie&gt;
  &lt;gebdate&gt;1960&lt;/gebdate&gt;
      &lt;project&gt;
          &lt;rol&gt;Projectmanager&lt;/rol&gt;
          &lt;opdrachtgever&gt;Apple&lt;/opdrachtgever&gt;
          &lt;result&gt;Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum &lt;/result&gt;
          &lt;time&gt;2012-2013&lt;/time&gt;
      &lt;/project&gt;
  
      &lt;project&gt;
          &lt;rol&gt;Teamleader&lt;/rol&gt;
          &lt;opdrachtgever&gt;Google&lt;/opdrachtgever&gt;
          &lt;result&gt;Lorum at google ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum &lt;/result&gt;
          &lt;time&gt;2011-2014&lt;/time&gt;
      &lt;/project&gt;
  </code></pre>
  
  <h2>Template HTML file including inline css</h2>
  
  <p>In real this will be a exported word document to a file in the format html filtered </p>
  
  <pre><code>&lt;table style="width: 100%;"&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;Profession&lt;/td&gt;
              &lt;td&gt;[professie]&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Date of birth&lt;/td&gt;
              &lt;td&gt;[gebdate]&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;project&gt;
  &lt;table style="width: 100%;"&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&amp;gt;&lt;/td&gt;
              &lt;td&gt;Rol&lt;/td&gt;
              &lt;td&gt;:&lt;/td&gt;
              &lt;td&gt;[rol]&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;Opdrachtgever&lt;/td&gt;
              &lt;td&gt;:&lt;/td&gt;
              &lt;td&gt;[opdrachtgever]&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;Resultaat&lt;/td&gt;
              &lt;td&gt;:&lt;/td&gt;
              &lt;td&gt;[result]&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;Datum&lt;/td&gt;
              &lt;td&gt;:&lt;/td&gt;
              &lt;td&gt;[time]&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>So I build a function to do this. For the moment i build pseudo code like this:</p>
  
  <pre><code>&lt;?php
  
  function generatemydocument ($path_to_content_file,$path_to_template_file,$path_to_output_file){
  
      if (!file_exists($path_to_xml_file)){
          return;
      }
      if (!file_exists($path_to_template_file)){
          return;
      }
      if (file_exists($path_to_output_file)){
          // make log message output file already exist
          return;
      }
  
      // read the file into a string
      $templatefile = readfileascompletestring ($path_to_template_file)
  
      // Search and replace all single dom elemelens with no children
  
      // Get all elements from dom document with no childs and put it in a array
      // Reallly nu clu how to do this yet ...
      $array_with_strings_to_replace = array("all elements from $path_to_content_file with NO children","all values for this element")
  
      // init
      $dbData = array(); 
      foreach ($array_with_strings_to_replace as $key =&gt; $value)
          $sanitizedValue = strip_tags(ucfirst(strtolower($value)));
          $templatefile = str_replace('{$'.$key.'}', $sanitizedValue, $templatefile);
          $dbData[$key] = mysql_real_escape_string($sanitizedValue);
          // add a line of code into a log file
      }   
  
      // Search and replace all nodes with the use of a dom translation
  
      $content = new DOMDocument();
      $content-&gt;loadXML($path_to_content_file);
  
      $template = new DOMDocument();
      $template -&gt;loadHTML($templatefile);
  
      // Create a new document
      $newdoc = new DOMDocument;
      $newdoc-&gt;formatOutput = true;
  
      // Set the template in the newdoc
      $newdoc = $template
  
      // Import the node, and all its children, to the document
      $node = $newdoc-&gt;importNode($node, true);
  
      // I think  I have to do some replacements here but I'm a little bit lost in here
  
      // And then append it to the "&lt;root&gt;" node
      $newdoc-&gt;documentElement-&gt;appendChild($node);
  
      $newdoc-&gt;saveHTML($path_to_output_file  );
  
  }
  ?&gt;
  </code></pre>
  
  <p>But the more I understand of this dom library of php I have some questions.</p>
  
  <p>1) Now, I do a string replace action for nodes which have no children nodes. I get the feeling this can be done with DOMNode DOMDocument::importNode and that the importnode just import the node with or without children nodes. Is that correct?</p>
  
  <p>2) I do not understand how to deal with the merging on a node in the content and in the template. Should I do a replace and search for this also?</p>
  
  <p>3) I do not have the idea that this is new in this world. Is there a library function which can do this already?</p>
  
  <p>The function should not contain any information from the dom documents itself, I can change the input variables of the function with information we need to merge this complete thing e.g a list with all names of the elements.</p>
  
  <p>Can this be done more easily?</p>
  
  <p>In the end this should be a module in drupal7 where users are selecting a node in the system (this will give content.xml) and this node will be a downloaded in a word document (template.html is the basis of this).</p>