php - 从数据库创建谷歌站点地图

标签 php xml dom

我正在尝试根据我的数据库提供的信息为 google 创建一个站点地图,一切正常,除非我尝试使用 image:image 和 image:loc 我在我的 xml 文件中收到此错误:

此页面包含以下错误:

error on line 8 at column 17: Namespace prefix image on image is not defined

下面是第一个错误之前的页面呈现。

我的代码:

//create the xml document
$xmlDoc = new DOMDocument('1.0');

//create the root element
$root = $xmlDoc->appendChild(
          $xmlDoc->createElement('urlset'));
$root->appendChild(
    $xmlDoc->createAttribute("xmlns"))->appendChild(
      $xmlDoc->createTextNode('http://www.sitemaps.org/schemas/sitemap/0.9'));       

foreach($r as $spirit){

$urlSpirit = 'http://urlroot.com' . $spirit['Category'] . '/' .  $spirit['subcategory'] . '/' . $spirit['permName'];
$imgSpirit = 'http://urlroot.com' . $spirit['picture'];
  //create a url element
  $urlTag = $root->appendChild(
              $xmlDoc->createElement("url"));

  //create the loc element
  $urlTag->appendChild(
    $xmlDoc->createElement("loc", $urlSpirit));

  //create the changefreq element
  $urlTag->appendChild(
    $xmlDoc->createElement("changefreq", 'weekly'));

  //create the priority element
  $urlTag->appendChild(
    $xmlDoc->createElement("priority", '1.0'));

  //create the lastmod element
  $urlTag->appendChild(
    $xmlDoc->createElement("lastmod", $spirit['lastReview']));


  //create the img element
  $imgTag = $urlTag->appendChild(
              $xmlDoc->createElement('image:image'));

   $imgTag->appendChild(
      $xmlDoc->createElement("image:loc", $imgSpirit));
}

header("Content-Type: text/plain");

//make the output pretty
$xmlDoc->formatOutput = true;

$xmlDoc->save('test.xml');

有什么想法吗?

最佳答案

您缺少图像命名空间,因此您需要添加:

$root->appendChild(
    $xmlDoc->createAttribute("xmlns:image"))->appendChild(
        $xmlDoc->createTextNode('http://www.google.com/schemas/sitemap-image/1.1'));

正如有人提到的,您可以在 Google documentation of images 中看到这一点.

关于php - 从数据库创建谷歌站点地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9754776/

相关文章:

java - 如何写入 XML 中的特定标记?

java - Hibernate无法创建表

javascript - 如何将元素之前的所有 div 移到元素之后?

javascript - 从 Firefox 插件(Webextension API)修改网页 : how to access the elements properly?

javascript - 如何从 d3 节点数据获取 dom svg 元素?

php - "page not found nginx/1.4.4"错误在除主页之外的每个页面上

javascript - ajax 响应不使用当前加载的资源 (php + jquery)

php - PHP会死在网页开发世界中吗?

javascript - 使用ajax发送多个变量的最佳实践

xml - 将 XML 类型序列化为文本时,PostgreSQL 不包含 XML 声明,不是吗?