I see many questions about this, but answers are not working for me!
I tried this:
$xml = new DomDocument();
$xml->load('sitemap.xml');
$linkforxml = "https://example.com/" . $filename. ".php";
$xpath = new DOMXPATH($xml);
$nodes = $xpath->query('/urlset/url[loc = "'.$linkforxml.'"]');
foreach($nodes as $node){
$node->parentNode->removeChild($node);
}
$xml->save('sitemap.xml');
And this is what is inside my sitemap file:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>https://example.com/test.php</loc>
<lastmod>2021-06-21T17:31:16+00:00</lastmod>
</url>
</urlset>
When I run this code the url is still in sitemap file and nothing happen. Anyone can tell me where is my problem?