【问题标题】:Google Webmaster sitemap issue with Tags and Header带有标签和标题的 Google 网站管理员站点地图问题
【发布时间】:2018-06-16 17:14:08
【问题描述】:

我正在尝试制作新闻站点地图,并且站点地图文件可以正常打开,但在 google 网站管理员工具测试中显示两个错误:

  1. 无效的 XML 标记
  2. 预期:您的站点地图或站点地图索引文件未正确声明命名空间。您的站点地图或站点地图索引文件未正确声明命名空间。预期:http://www.w3.org/1999/xhtml 发现:http://www.sitemaps.org/schemas/sitemap/0.9

任何帮助提前谢谢

header("Content-Type: application/xml; charset=utf-8");

echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; 

echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xlink="http://www.w3.org/1999/xlink" 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">' . PHP_EOL;

$base_url = "https://www.example.com";

while($row = mysqli_fetch_array($sitegrab))
{
    echo '<url>' . PHP_EOL;
    echo '<loc>'.$base_url.'</loc>' . PHP_EOL;
    echo '<news:name>'.$row['Website'].'</news:name>' . PHP_EOL;
    echo '<news:link>'.$row['Link'].'</news:link>' . PHP_EOL;
    echo '<changefreq>daily</changefreq>' . PHP_EOL;
    echo '<priority>1</priority>' . PHP_EOL;
    echo '</url>' . PHP_EOL;
}

echo '</urlset>' . PHP_EOL;
?>

【问题讨论】:

    标签: php xml sitemap google-search-console


    【解决方案1】:

    确保在将 XML 构建为文本时正确转义特殊字符。例如,&amp;amp; 很可能是 URL 的一部分,需要在 XML 字符串中转义为 &amp;amp;

    更好的解决方案是使用像 XMLWriter 这样的 XML API 来创建 XML。它会处理转义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      相关资源
      最近更新 更多