2011-11-03 44 views
0

我寫這個小類以下,其產生XML sitemap雖然當我試圖把它添加到谷歌網站管理員,我得到錯誤:生成谷歌網站地圖沒有驗證

Sitemaps網址:http://www.moto-trek.co.uk/sitemap/xml

不支持的文件格式 您的Sitemap似乎沒有受支持的格式。請確保它符合我們的網站地圖指南並重新提交。

<?php 

class Frontend_Sitemap_Xml extends Cms_Controller { 

    /** 
    * Intercept special function actions and dispatch them. 
    */ 
    public function postDispatch() {  
     $db = Cms_Db_Connections::getInstance()->getConnection(); 
     $oFront = $this->getFrontController(); 
     $oUrl = Cms_Url::getInstance(); 
     $oCore = Cms_Core::getInstance(); 

     $absoDomPath = $oFront->getDomain() . $oFront->getHome(); 

     $pDom = new DOMDocument(); 

     $pXML = $pDom->createElement('xml'); 
     $pXML->setAttribute('version', '1.0'); 
     $pXML->setAttribute('encoding', 'UTF-8'); 

     // Finally we append the attribute to the XML tree using appendChild 
     $pDom->appendChild($pXML); 

     $pUrlset = $pDom->createElement('urlset'); 
     $pUrlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); 
     $pXML->appendChild($pUrlset); 

     // FETCH content and section items 
     $array = $this->getDataset("sitemap")->toArray(); 
     foreach($array["sitemap"]['rows'] as $row) { 
      try {    
       $content_id = $row['id']['fvalue']; 
       $url = "http://".$absoDomPath.$oUrl->forContent($content_id); 

       $pUrl = $pDom->createElement('url'); 

       $pLoc  = $pDom->createElement('loc', $url); 
       $pLastmod = $pDom->createElement('lastmod', gmdate('Y-m-d\TH:i:s', strtotime($row['modified']['value']))); 
       $pChangefreq = $pDom->createElement('changefreq', ($row['changefreq']['fvalue'] != "")?$row['changefreq']['fvalue']:'monthly'); 
       $pPriority = $pDom->createElement('priority', ($row['priority']['fvalue'])?$row['priority']['fvalue']:'0.5'); 

       $pUrl->appendChild($pLoc); 
       $pUrl->appendChild($pLastmod); 
       $pUrl->appendChild($pChangefreq); 
       $pUrl->appendChild($pPriority); 

       $pUrlset->appendChild($pUrl); 
      } catch(Exception $e) { 
       throw($e);   
      } 
     } 

     // Set content type to XML, thus forcing the browser to render is as XML 
     header('Content-type: text/xml'); 

     // Here we simply dump the XML tree to a string and output it to the browser 
     // We could use one of the other save methods to save the tree as a HTML string 
     // XML file or HTML file. 
     echo $pDom->saveXML(); 

    } 
} 
?> 

回答

0

urlset應該由root元素,但在你的情況下,它是xml。所以將urlset直接附加到domdocument應該可以解決您的問題。

$pDom = new DOMDocument('1.0','UTF-8'); 

$pUrlset = $pDom->createElement('urlset'); 
$pUrlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); 

foreach(){ } 

$pDom->appendChild($pUrlset); 
echo $pDom->saveXML(); 
+0

好,謝謝,我試圖消除的createElement(「XML」),雖然這將導致XML網站地圖不顯示 –

+0

我忘了提,但編碼及版本應移到DOMDocument'的'構造,看示例代碼,它工作正常。 –

+0

工作出色,謝謝 –