从数据库创建Google Sitemap

Cla*_*ont 3 php xml dom

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

此页面包含以下错误:

第17行第8行的错误:未定义图像上的命名空间前缀图像

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

我的代码:

//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');
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

小智 7

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

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

正如有人提到的,您可以在Google图片文档中看到这一点.