title: Difference between DOMDocument::getElementsByTagName and DOMXPath::query?
body: <p>DOMDocument::getElementsByTagName() and DOMXPath::query() both return a DOMNodeList. Yet, they seem to work differently:</p>
  
  <pre><code>// Works as expected, removes all scripts
  $r=$doc-&gt;getElementsByTagName('script');
  while($r-&gt;length){
      $el=$r-&gt;item(0);
      $el-&gt;parentNode-&gt;removeChild($el);
  }
  
  // Error
  $r=$xpath-&gt;query('//comment()');
  while($r-&gt;length){
      $el=$r-&gt;item(0);
      $el-&gt;parentNode-&gt;removeChild($el);
  }
  
  // Works as expected, removes all comments
  $r=$xpath-&gt;query('//comment()');
  foreach($r as $el){
      $el-&gt;parentNode-&gt;removeChild($el);
  
  }
  </code></pre>
  
  <p>Why are they different? The DOMNodeList returned by <code>getElementsByTagName()</code> automatically shifts the indexes down when the first node is removed while the DOMNodeList returned by <code>query()</code> simply makes the first index null. Are there other differences?</p>