Skip to content

访问 https://www.o2sir.cn/sitemap.xml 时,浏览器或爬虫报错 #3724

@queyangdeyangxiansheng

Description

@queyangdeyangxiansheng

XML Sitemap 解析错误问题总结

问题描述:
访问 https://www.o2sir.cn/sitemap.xml 时,浏览器或爬虫报错:

Image
error on line 30 at column 61: EntityRef: expecting ';'

原因分析:

  • XML 文件中出现了未转义的特殊字符,尤其是 &
  • 在 XML 中,& 必须写成 &,否则解析器会报错。
  • 可能出现在 URL、文章标题、slug 或其他字段中。
  • 例如,URL 中的参数 ?a=1&b=2,应写为 ?a=1&b=2

影响范围:

  • 导致搜索引擎无法正确解析 sitemap,影响网站收录和 SEO。
  • 影响第三方工具抓取和分析。

解决方案

建议修复方法:

  1. 在生成 sitemap.xml 的代码(如 lib/sitemap.xml.jscreateSitemapXml 函数)中,增加 XML 字符转义处理。
  2. 对所有输出到 XML 的内容(包括 URL、标题等)进行如下转义:
    • &&
    • <&lt;
    • >&gt;
    • "&quot;
    • '&apos;
  3. 可以封装一个 escapeXml 函数,在生成 XML 内容时统一调用。

示例代码:

function escapeXml(str) {
  return str
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&apos;');
}

// 使用示例
const url = escapeXml(item.url);
const title = escapeXml(item.title);

结论:
只要对所有 XML 输出内容进行特殊字符转义,即可解决该解析错误,保证 sitemap.xml 能被正常解析和抓取。

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions