Sitemap invalid XML: fix ampersands without changing URLs
Repair sitemap XML errors caused by unescaped ampersands, distinguish XML escaping from URL encoding, and test the generated file and parsed destinations.
By Crawlito · 5 min read
In short
- A query separator must be escaped when its URL is written as XML text.
- XML escaping and URL percent encoding solve different problems.
- Parse the sitemap and compare the extracted URL with the intended destination.
On this page
If a sitemap fails XML validation at a URL containing an ampersand, escape the ampersand as XML text without changing the URL's meaning. In a loc element, write & where the actual URL contains &. Then parse the generated file and confirm that the extracted destination is the original URL, not a double-escaped or otherwise changed address.
A sitemap that returns 200 can still be invalid. Fetching a file, parsing its XML, and evaluating its listed pages are separate steps. Diagnose the step that failed before changing which URLs you publish.
Locate the first parser error
Download the public sitemap as a file and retain the response headers:
curl -sS -L -D sitemap-headers.txt -o sitemap.xml \
'https://www.example.com/sitemap.xml'
python3 -c 'import xml.etree.ElementTree as E; E.parse("sitemap.xml"); print("XML parsed")'Python's built-in XML parser can report a line and column for malformed input. That points to the generated file, which may have different line breaks from your CMS source.
Inspect the first reported problem before trying to repair every URL. One malformed element can prevent the document from parsing, and later errors may be consequences of the first.
Confirm that the downloaded body is actually XML. A login page, challenge page, or HTML error template requires a delivery repair rather than escaping. The sitemap fetch troubleshooting guide covers that earlier boundary.
Save a small excerpt around the failing entry. Include the URL's intended meaning in the ticket so a developer does not “fix” the parser by turning a query separator into part of a parameter value.
Separate XML escaping from URL encoding
The Sitemaps protocol requires UTF-8 and entity-escaped values. Its examples distinguish URL encoding from XML escaping. Both layers can appear in the same entry.
For a hypothetical public guide, the browser URL might be:
https://www.example.com/guide?edition=public&language=enThe XML text should represent it as:
<loc>https://www.example.com/guide?edition=public&language=en</loc>After XML parsing, the extracted text contains a normal ampersand. That is the value a consumer should use as the URL.
Replacing the separator with %26 changes a different layer. Depending on the application, it can turn two query parameters into one value. Replacing an already escaped value again can produce &amp;, which parses to the wrong text.
Do not judge correctness only by how the XML looks in a browser. A browser may render or style the document. Inspect the raw bytes and the parsed value, then open the destination that the parser actually produced.
Fix the serializer instead of patching every output file
Find where the sitemap is built. A proper XML serializer should receive the intended URL as data and handle escaping for the XML context. Hand-built string concatenation often causes the first defect.
Check whether the input is already HTML-escaped. CMS fields sometimes store a link copied from markup, while a sitemap generator expects a raw URL. Passing escaped text into a serializer can produce double escaping.
Write down the input and output contract:
| Stage | Expected value |
|---|---|
| URL builder | The intended navigable URL |
| XML serializer input | That URL as text |
| Published XML | Correct XML representation |
| Parsed loc | The intended navigable URL again |
Keep the repair at the boundary that violates this contract. A global search-and-replace on the published sitemap can hide one error while damaging values that were already correct.
If multiple systems generate child sitemaps, identify each owner. Fixing the index file does not repair malformed product or article files. The sitemap index guide helps trace those child files systematically.
Test meaningful URL cases
Use a short set of actual URL shapes your site supports. Include a plain path, a URL with two query parameters, a non-ASCII path if relevant, and a value containing a character that needs URL encoding.
Do not add arbitrary parameter combinations to the sitemap just to exercise the serializer. The test fixture can contain examples without changing the set of pages you intend to publish.
For each case, parse the generated XML and compare the loc text with the URL builder's output. Then fetch the intended public URL and confirm its content. This catches both malformed XML and valid XML that points to the wrong page.
A useful failure example is a product selector that loses its second parameter. The page may still return 200, but show the default variant. A parser-only check would miss that semantic change.
The URL parameter guide explains why not every working parameter URL belongs in a sitemap. Escaping repairs serialization; it does not decide canonical selection or content value.
Verify the published artifact
After deploying the generator change, download the live sitemap again. Run the parser against that file, not just a local fixture. Check every child file that uses the changed code.
Compare a few parsed destinations with the intended pages. Confirm status, final URL, and a distinctive content marker. If a deployment cache serves the old sitemap, the local success is not yet the public result.
Also inspect the file's beginning and end. A truncated upload or debug message before the XML can create a different parser failure. Preserve the exact downloaded artifact when reporting it.
Only after the public document parses should you use the relevant webmaster report to assess subsequent processing. A submitted sitemap can remain in an earlier error state until the service retrieves it again.
Record the deployment time, public file URL, parser result, and tested destinations. Those four items let another person distinguish a fixed generator from a still-stale delivery path without rerunning the entire content audit.
Prevent content imports from reintroducing the error
Review how editors enter or import URLs. A field meant for a URL should not silently accept an entire anchor element or an HTML-escaped string without a defined conversion step.
For a CSV import, preserve the raw input alongside the normalized URL during a test run. Compare examples containing multiple parameters before applying the import broadly. Do not repair strings by removing all unusual characters; that can change valid destinations.
If an external plugin generates the sitemap, reproduce the failing entry in a minimal example and use the supported configuration or update path. Editing a generated file manually may be overwritten on the next publication.
Keep a small regression fixture with the exact shape that failed. It should verify the parsed destination, not merely search for the string amp. That test expresses the real requirement: the sitemap must carry the intended URL through a valid XML document.
Give content owners a plain explanation of the fix. They should keep entering normal URLs; the publishing system should handle the document format.
For the repaired example, retain the raw URL, serialized XML entry, and parsed loc text together. A reviewer can then see that the formatting changed while the destination stayed the same. Include that comparison when handing the generator back to its owner.
Frequently asked questions
Should an ampersand be percent-encoded in every sitemap URL?
No. A query separator and an ampersand inside a parameter value have different meanings. Build the correct URL first, then escape it for XML.
Can valid XML still contain broken URLs?
Yes. XML parsing checks the document format. Fetch and inspect representative parsed destinations to verify their meaning.
Does a browser displaying the sitemap prove it is valid?
No. Use a parser on the downloaded public file and inspect its extracted loc values.
Run an affected destination through the free Crawlito check after fixing the sitemap to confirm that discovery leads to a usable page.
Filed under Structure and schema. Topics: sitemaps, troubleshooting, technical-seo