生成的谷歌网站地图不验证


Generated Google Site map not validating

我写了下面这个小类生成一个XML站点地图虽然当我试图将它添加到Google Webmaster时,我得到错误:

Sitemap URL: 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();
    }   
}
?>

urlset应该是根元素,但在您的情况下是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();