Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Google Sitemap from Database

Tags:

dom

php

xml

I am trying to create a sitemap for google from info supplied from my database, everything works except when I try and use image:image and image:loc I get this error in my xml file:

This page contains the following errors:

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

Below is a rendering of the page up to the first error.

My Code:

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

Any Ideas?

like image 562
Claremont Avatar asked Mar 17 '12 23:03

Claremont


People also ask

Does Google have a sitemap generator?

Google Sitemap Generator is a server plug-in that can be installed on both Linux/Apache and Microsoft IIS Windows-based servers. As with other server-side plug-ins, you will need to have administrative access to the server to install it.


1 Answers

Your missing the image namespace so you need to add this:

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

As someone mentioned you can see this in the Google documentation of images.

like image 137
David Odere Avatar answered Sep 30 '22 13:09

David Odere