Search code examples
domdocument

Remove parent nodes if contains tag


I'm trying to remove url elements from my sitemap if they contain a priority tag. What am I doing wrong?

Script:

<?php

$xml = new DOMDocument;
$xml->load('sitemap.xml');

$priorities = $xml->getElementsByTagName('priority');
foreach($priorities as $priority){
    $xml->removeChild($priority);
    }

echo $xml->saveXML();

Existing sitemap:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>https://www.example.tld/</loc></url>
<url><loc>https://www.example.tld/file</loc></url>
<url><loc>https://www.example.tld/folder/</loc><priority>0.7</priority></url>
<url><loc>https://www.example.tld/folder2/</loc><priority>0.3</priority></url>
</urlset>

Desired sitemap:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>https://www.example.tld/</loc></url>
<url><loc>https://www.example.tld/file</loc></url>
</urlset>

Solution

  • You have to use the parentNode of $priority instead of $xml to remove the child.

    Instead of using a foreach, you can loop the collection using a for loop decrementing the value of $i

    $xml = new DOMDocument;
    $xml->load('sitemap.xml');
    
    $priorities = $xml->getElementsByTagName('priority');
    for ($i = $priorities->length - 1; $i >= 0; $i--) {
        $priorities[$i]->parentNode->removeChild($priorities[$i]);
    }
    echo $xml->saveXML();