2012-03-17 39 views
2

我想創建一個從我的數據庫提供的信息穀歌的網站地圖,除了當我嘗試和使用圖像時,一切正常工作:圖像和圖像:祿我在我的xml文件中得到這個錯誤:從數據庫創建谷歌網站地圖

此頁面包含以下錯誤:

第8行誤差在柱17:使圖像命名空間前綴的圖像不是 定義

下面是向上翻頁的渲染到第一誤差。

我的代碼:

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

什麼想法?

+1

你問什麼問題?這個錯誤是因爲你正在創建一個元素'image:image'(和後來的'image:loc'),但是沒有設置'image'命名空間前綴。 – 2012-03-17 23:44:47

+0

但我不想設置一個命名空間,我只是跟隨谷歌的例子。我想我的問題是如何讓它不認爲它是一個命名空間,只是打印它。 – Claremont 2012-03-17 23:48:38

+1

你沒有緊密地跟蹤它。形式爲'prefix:local_part'的任何元素都使用一個名稱空間前綴,至少對於某些工具,您需要聲明它們。 [Google站點地圖中的Google圖片文檔](http://support.google.com/webmasters/bin/answer.py?hl=zh_CN&answer=178636)包含您的相關名稱空間前綴聲明。 – 2012-03-17 23:53:03

回答

5

你錯過了圖像的命名空間,所以你需要補充一點:

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

正如有人提到你可以在Google documentation of images看到這一點。