我想用 PHP 删除 XML 中的一个节点,但它不起作用

时间:2021-06-21 13:43:57

标签: php xml xpath

我看到很多关于此的问题,但答案对我不起作用!

我试过了:

$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');

这是我的站点地图文件中的内容:

<?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>

当我运行这段代码时,url 仍然在站点地图文件中,什么也没有发生。 谁能告诉我我的问题在哪里?

0 个答案:

没有答案
相关问题