XML 错误:仅允许一个顶级元素

发布于 2024-08-16 12:41:57 字数 841 浏览 6 评论 0原文

我收到 XML 错误:“XML 文档中只允许有一个顶级元素。”当我尝试在 PHP 中运行站点地图脚本时:

$num_rows = mysql_num_rows(mysql_query("SELECT * FROM pages_content WHERE date < CURRENT_TIMESTAMP"));   
$result = mysql_query("SELECT * FROM pages_content WHERE date < CURRENT_TIMESTAMP ORDER BY id DESC") or die("Query failed");
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">';       
for($i=0;$i<$num_rows; $i++) {      
   $url_product = 'http://www.hostcule.com/'.mysql_result($result,$i,"title");   

    echo'  
       <url>   
         <loc>'.$url_product.'</loc>      
         <changefreq>monthly</changefreq>   
         <priority>0.8</priority>   
      </url>   
   ';   

echo '</urlset>'; } 

有什么问题吗?

I recieve XML error: "Only one top level element is allowed in an XML document." when I try to run my sitemap script in PHP:

$num_rows = mysql_num_rows(mysql_query("SELECT * FROM pages_content WHERE date < CURRENT_TIMESTAMP"));   
$result = mysql_query("SELECT * FROM pages_content WHERE date < CURRENT_TIMESTAMP ORDER BY id DESC") or die("Query failed");
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">';       
for($i=0;$i<$num_rows; $i++) {      
   $url_product = 'http://www.hostcule.com/'.mysql_result($result,$i,"title");   

    echo'  
       <url>   
         <loc>'.$url_product.'</loc>      
         <changefreq>monthly</changefreq>   
         <priority>0.8</priority>   
      </url>   
   ';   

echo '</urlset>'; } 

What's wrong with it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞陪衬 2024-08-23 12:41:57

您需要将结束 ' 移到 for 循环之外。

You need to move the closing '</urlset'> outside of the for loop.

孤独陪着我 2024-08-23 12:41:57

此行是错误的:

echo '</urlset>'; } 

您需要:

}
echo '</urlset>';  

当您多次关闭顶级标签时,您会收到该错误。

This line is wrong:

echo '</urlset>'; } 

You need:

}
echo '</urlset>';  

As you are closing the top level tag multiple times, you are getting that error.

花心好男孩 2024-08-23 12:41:57

您需要将大括号 } 移到 echo 之前。就像这样:

$num_rows = mysql_num_rows(mysql_query("SELECT * FROM pages_content WHERE date < CURRENT_TIMESTAMP"));   
$result = mysql_query("SELECT * FROM pages_content WHERE date < CURRENT_TIMESTAMP ORDER BY id DESC") or die("Query failed");
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">';       
for($i=0;$i<$num_rows; $i++) {      
   $url_product = 'http://www.hostcule.com/'.mysql_result($result,$i,"title");   

    echo'  
       <url>   
         <loc>'.$url_product.'</loc>      
         <changefreq>monthly</changefreq>   
         <priority>0.8</priority>   
      </url>   
   ';   
}//<=To here
echo '</urlset>'; // move this one =>} 

You need to move the curly bracket } before the echo. Like so:

$num_rows = mysql_num_rows(mysql_query("SELECT * FROM pages_content WHERE date < CURRENT_TIMESTAMP"));   
$result = mysql_query("SELECT * FROM pages_content WHERE date < CURRENT_TIMESTAMP ORDER BY id DESC") or die("Query failed");
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">';       
for($i=0;$i<$num_rows; $i++) {      
   $url_product = 'http://www.hostcule.com/'.mysql_result($result,$i,"title");   

    echo'  
       <url>   
         <loc>'.$url_product.'</loc>      
         <changefreq>monthly</changefreq>   
         <priority>0.8</priority>   
      </url>   
   ';   
}//<=To here
echo '</urlset>'; // move this one =>} 
葬花如无物 2024-08-23 12:41:57

PHP 是一种模板语言。使用它来创建输出,而不是乱七八糟地连接字符串。例如:

<?php
    $result= mysql_query('SELECT * FROM pages_content WHERE date<CURRENT_TIMESTAMP ORDER BY id DESC') or die('Query failed');
?>
<urlset>
    <?php while ($row= mysql_fetch_assoc($result)) { ?>
        <url>   
            <loc>http://www.hostcule.com/<?php urlencode($row['title']) ?></loc>      
            <changefreq>monthly</changefreq>   
            <priority>0.8</priority>   
        </url>   
    <?php } ?>
</urlset>

通过像这样一致的缩进,诸如将 放在错误位置之类的错误会立即变得明显,而不是调试起来很痛苦。

PHP is a templating language. Use it to create your output instead of messing around with concatenating strings. eg.:

<?php
    $result= mysql_query('SELECT * FROM pages_content WHERE date<CURRENT_TIMESTAMP ORDER BY id DESC') or die('Query failed');
?>
<urlset>
    <?php while ($row= mysql_fetch_assoc($result)) { ?>
        <url>   
            <loc>http://www.hostcule.com/<?php urlencode($row['title']) ?></loc>      
            <changefreq>monthly</changefreq>   
            <priority>0.8</priority>   
        </url>   
    <?php } ?>
</urlset>

With consistent indentation like this, mistakes like getting the </urlset> in the wrong place become immediately obvious instead of a pain to debug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文