XML解析格式无效问题:为何部分商品链接报错及修复方法
Hey there, let's break down why your product_id=8111 URL works fine in the sitemap but product_id=1100 throws a parsing error. The root cause almost always boils down to unescaped special characters in the URL—XML is super strict about this!
Why the Discrepancy?
Look at your working URL:
<loc>http://localhost/index.php?route=product/product&product_id=8111</loc>
Notice the & instead of a plain &? That's the key. XML treats & as the start of an entity reference (like & itself), so if you write a raw & in your URL, the parser gets confused—it thinks you're trying to reference an XML entity that doesn't exist, hence the "not well-formed" error.
Chances are your product_id=1100 link looks like this (which is invalid):
<loc>http://localhost/index.php?route=product/product&product_id=1100</loc>
The raw & here breaks XML parsing entirely.
Other possible (though less likely) issues:
- The URL contains other forbidden XML characters like
<,>,", or'without escaping - There are hidden spaces or weird invisible characters at the end of the URL
Step-by-Step Fixes
Escape All Special Characters in URLs
For sitemap URLs, you must escape these characters to comply with XML rules:&→&(this is the one tripping you up)<→<>→>"→"'→'
Your correctedproduct_id=1100line should be:
<loc>http://localhost/index.php?route=product/product&product_id=1100</loc>Validate Your Entire Sitemap
After fixing the URLs, run your sitemap through an XML validator to catch any other syntax issues. You can use local XML validation tools to avoid sharing sensitive site data.Automate Escaping If You Generate Sitemaps Programmatically
If your sitemap is auto-generated (e.g., with PHP), add a step to escape URLs before writing them to XML. In PHP, thehtmlspecialchars()function with the right flags does this perfectly:$raw_url = "http://localhost/index.php?route=product/product&product_id=1100"; $escaped_url = htmlspecialchars($raw_url, ENT_XML1, 'UTF-8'); echo "<loc>{$escaped_url}</loc>";This ensures you never forget to escape special characters manually.
Quick Extra Check
You already have the correct encoding="UTF-8" declaration at the top of your XML, which is great. One tiny note: while your <lastmod> format (2019-05-13 13:07:07) won't cause parsing errors, the official sitemap protocol recommends using the YYYY-MM-DDThh:mm:ssTZD format (e.g., 2019-05-13T13:07:07+00:00) for better compatibility with search engines.
内容的提问来源于stack exchange,提问作者Ivan

