Search code examples
phpdomdocumentremovechilddomxpath

Correctly deleting child elements in PHP using DOMDocument and DOMXPath


This was previous another question, but we won't talk about that. I am isolating a number of sections in a third party HTML document. When matching some, I need to remove certain tags from the result. The code I found for this on SO was:

$name = $xpath->query("//div[@class='leftColBig']//h3")->item(0);
// remove <span>
foreach($xpath->query("//span", $name) as $node)
    $node->parentNode->removeChild($node);

This has the unfortunate side effect of not just deleting the child from $name, but the entire DOMDocument :( How can I isolate the removeChild just to the section I found using the query.


Solution

  • Instead of:

    $xpath->query("//span", $name)
    

    Do:

    $xpath->query("span", $name)
    

    //nodename matches all nodes no matter what their parent is. $contextnode is ignored when your query starts with //.