title: How to alter DOM with xmldom by XPath in node.js?
body: <p>I am trying to alter a DOM structure in node.js. I can load the XML string and alter it with the native methods in xmldom (<a href="https://github.com/jindw/xmldom" rel="nofollow">https://github.com/jindw/xmldom</a>), but when I load XPath (<a href="https://github.com/goto100/xpath" rel="nofollow">https://github.com/goto100/xpath</a>) and try to alter the DOM via that selector, it does not work.</p>
  
  <p>Is there another way to do this out there? The requirements are:</p>
  
  <ul>
  <li>Must work both in the browser and server side (pure js?)</li>
  <li>Cannot use eval or other code execution stuff (for security)</li>
  </ul>
  
  <p>Example code to show how I am trying today below, maybe I simply miss something basic?</p>
  
  <pre><code>var xpath = require('xpath'),
      dom   = require('xmldom').DOMParser;
  
  var xml = '&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;blah&lt;/title&gt;&lt;/head&gt;&lt;body id="test"&gt;blubb&lt;/body&gt;&lt;/html&gt;';
  var doc = new dom().parseFromString(xml);
  
  var bodyByXpath = xpath.select('//*[@id = "test"]', doc);
  var bodyById    = doc.getElementById('test');
  var h1          = doc.createElement('h1').appendChild(doc.createTextNode('title'));
  
  // Works fine :)
  bodyById.appendChild(h1);
  
  // Does not work :(
  bodyByXpath.appendChild(h1);
  
  console.log(doc.toString());
  </code></pre>