我在下面编写了这个小课程,生成XML sitemap,但是当我尝试将其添加到Google网站管理员时,我收到错误:
站点地图网址: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 :(得分: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();