提问人:Jamaal 提问时间:7/22/2023 最后编辑:Jamaal 更新时间:7/22/2023 访问量:130
如何删除xml网站地图开头的空格:<?xml version=“1.0” encoding=“UTF-8”?>?
How to remove whitespace in the beginning in xml sitemap : <?xml version="1.0" encoding="UTF-8"?>?
问:
我在浏览器上访问站点地图.xml时出现此错误
error on line 1 at column 7: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.
经过一番调查,我了解到开始空格导致了问题。
<?xml version="1.0" encoding="UTF-8"?>
<urlset
以下代码生成 xml 标记:
<?php
class Sitemap extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('sitemap_', 'sitemap');
//$this->load->model('deals_', 'deals');
//$this->load->library('ravresponse');
}
public function xml() {
$website_id = (int)$this->ravconfig->get('config_website_id');
//$language_id = (int)$this->ravconfig->get('config_language_id');
if (isset($this->request->server['HTTPS']) && $this->request->server['HTTPS']) {
$server = $this->ravconfig->get('config_ssl');
} else {
$server = $this->ravconfig->get('config_url');
}
header('Content-Type: application/xml');
$output = '';
$output .= '<?xml version="1.0" encoding="UTF-8"?>';
$output .= "\n".'<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
// Retrieve languages assigned to website
$raw_languages = $this->sitemap->getAssignedLanguages($website_id);
if($raw_languages) {
$raw_languages = unserialize($raw_languages['value']);
$languages = $raw_languages['language'];
}
//print_r($languages);
// Home Page
$output .= "\n\t".'<url>';
$output .= "\n\t\t".'<loc>' . $server. '</loc>';
$output .= "\n\t\t".'<changefreq>weekly</changefreq>';
$output .= "\n\t\t".'<priority>1.00</priority>';
$output .= "\n\t".'</url>';
// Information Pages
$information_pages = $this->sitemap->getInformationPages($website_id);
if($information_pages) {
foreach($information_pages as $information_page) {
foreach($languages as $key => $language_id) {
$output .= "\n\t".'<url>';
$output .= "\n\t\t".'<loc>' . $this->link->geturl('information_id=' . $information_page['information_id'], "", $language_id, $website_id). '</loc>';
$output .= "\n\t\t".'<changefreq>weekly</changefreq>';
$output .= "\n\t\t".'<priority>0.70</priority>';
$output .= "\n\t".'</url>';
}
}
}
// Independent Pages
$independent_pages = $this->sitemap->getIndependentPages($website_id);
if($independent_pages) {
foreach($independent_pages as $independent_page) {
foreach($languages as $key => $language_id) {
$output .= "\n\t".'<url>';
$output .= "\n\t\t".'<loc>' . $this->link->geturl('independent_page_id=' . $independent_page['independent_page_id'], "", $language_id, $website_id). '</loc>';
$output .= "\n\t\t".'<changefreq>weekly</changefreq>';
$output .= "\n\t\t".'<priority>0.70</priority>';
$output .= "\n\t".'</url>';
}
}
}
// Destination Pages
$destination_pages = $this->sitemap->getDestinationPages($website_id);
if($destination_pages) {
foreach($destination_pages as $destination_page) {
foreach($languages as $key => $language_id) {
$output .= "\n\t".'<url>';
$output .= "\n\t\t".'<loc>' . $this->link->geturl('destination_id=' . $destination_page['destination_id'], "", $language_id, $website_id). '</loc>';
$output .= "\n\t\t".'<changefreq>weekly</changefreq>';
$output .= "\n\t\t".'<priority>0.70</priority>';
$output .= "\n\t".'</url>';
}
}
}
// Category Pages
$category_pages = $this->sitemap->getCategoryPages($website_id);
if($category_pages) {
foreach($category_pages as $category_page) {
foreach($languages as $key => $language_id) {
$output .= "\n\t".'<url>';
$output .= "\n\t\t".'<loc>' . $this->link->geturl('category_id=' . $category_page['category_id'], "", $language_id, $website_id). '</loc>';
$output .= "\n\t\t".'<changefreq>weekly</changefreq>';
$output .= "\n\t\t".'<priority>0.70</priority>';
$output .= "\n\t".'</url>';
}
}
}
// Product Pages
if($category_pages) {
foreach($category_pages as $category_page) {
$product_pages = $this->sitemap->getProductPages($category_page['category_id'], $website_id);
foreach($product_pages as $product_page) {
foreach($languages as $key => $language_id) {
$output .= "\n\t".'<url>';
$output .= "\n\t\t".'<loc>' . $this->link->geturl('product_id=' . $product_page['product_id'], "", $language_id, $website_id). '</loc>';
$output .= "\n\t\t".'<changefreq>weekly</changefreq>';
$output .= "\n\t\t".'<priority>0.70</priority>';
$output .= "\n\t".'</url>';
}
}
}
}
// Blog Pages
$blog_pages = $this->sitemap->getBlogPages($website_id);
if($blog_pages) {
foreach($blog_pages as $blog_page) {
$output .= "\n\t".'<url>';
$output .= "\n\t\t".'<loc>' . $this->link->geturl('blog_id=' . $blog_page['blog_id'], "", $blog_page['language_id'], $website_id). '</loc>';
$output .= "\n\t\t".'<changefreq>weekly</changefreq>';
$output .= "\n\t\t".'<priority>0.70</priority>';
$output .= "\n\t".'</url>';
}
}
$output .= "\n".'</urlset>';
print($output);
}
}
请问是什么导致了这个额外的空间吗?
我尝试删除代码破译行中的空格,但没有运气。 还试图呼应第一个标签,但仍创建空间。
答:
0赞
Markus Zeller
7/22/2023
#1
我想你已经有这个班级的空间了。因此,在包含此类之前检查所有其他文件。
大多数情况下,当您用换行符以 succeeded 结束文件时,它就会出现。?>
应始终在文件末尾省略,以防止输出(缓冲区)出现任何尾随空格。?>
也可以参考: https://www.php.net/manual/en/language.basic-syntax.phptags.php
评论
$output = '';
是多余的。这不会导致问题,但请尝试删除该行,看看它是否能修复。入手$output = '<?xml.....';
DOMDocument
header()
print($output);
echo "1";
echo "2";