Php 从数据库创建Google站点地图

Php 从数据库创建Google站点地图,php,xml,dom,Php,Xml,Dom,我正在尝试根据我的数据库提供的信息为google创建一个站点地图,除了我尝试使用image:image和image:loc时,其他一切都正常。我的xml文件中出现以下错误: 此页面包含以下错误: 第17列第8行出错:映像上的名称空间前缀映像不正确 明确的 下面是第一个错误之前的页面呈现 我的代码: //create the xml document $xmlDoc = new DOMDocument('1.0'); //create the root element $root = $xmlD

我正在尝试根据我的数据库提供的信息为google创建一个站点地图,除了我尝试使用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');

有什么想法吗?

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

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

正如有人提到的,您可以在中看到这一点。

您在问什么问题?错误是因为您正在创建元素
image:image
(以及随后的
image:loc
),但尚未设置
image
名称空间前缀。但我并不是在尝试设置名称空间,我只是在遵循google的示例。我想我的问题是,我如何使它不认为它是一个名称空间,而只是将其打印出来。您没有足够密切地关注它。表单
prefix:local\u part
中的任何元素都使用了名称空间前缀,至少对于某些工具,您需要声明它们。包含您的相关名称空间前缀声明。啊,我现在明白了,我一定错过了,谢谢