On this page
An ampersand in a sitemap URL usually separates query parameters. In XML, however, a literal & has a special meaning and must be escaped as &. This guide shows how to represent the URL in a sitemap and check both the XML and its destinations.
Key Takeaways
- Write a query-parameter separator as
&inside a sitemap’s<loc>element. - The actual URL still contains
&; XML escaping is only its representation in the file. - Do not replace a parameter separator with
%26: that represents an ampersand within URL data, not a separator. - Check XML syntax first, then check the extracted URLs themselves.
An ampersand in a sitemap URL
Suppose the destination URL is:
https://example.com/products?category=shoes&color=blue
In a sitemap, write it as:
<loc>https://example.com/products?category=shoes&color=blue</loc>
The XML parser interprets & as one ampersand. The resulting URL is the first one above, with &color=blue. A browser navigating to that extracted URL does not need the XML entity spelling.
XML escaping and URL encoding solve different problems. For example, %26 can represent an ampersand that is part of a query value. Preserve any necessary percent-encoding in the URL, and then escape the URL for XML when placing it in the sitemap.
Advertisement
Minimal sitemap example
Save the following as sitemap.xml in UTF-8 encoding. The hostname is a fictional example; replace it with a destination you control when checking a real sitemap.
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/products?category=shoes&color=blue</loc>
</url>
</urlset>
If you generate the file programmatically, give an XML serializer the actual URL containing &. Let the serializer escape it for XML; do not pre-escape it and then serialize it again. Otherwise, & can become &amp;, and the extracted URL will contain the unwanted text &.
Validate the XML and destination URLs
These checks serve different purposes: well-formed XML can still list a broken or unintended URL. The following example uses Python’s standard library and has not been executed in this environment.
1. Parse the sitemap and extract its URLs
With sitemap.xml in your current directory, run:
python3 - <<'PY'
import sys
import xml.etree.ElementTree as ET
from urllib.parse import urlsplit
namespace = {'sm': 'http://www.sitemaps.org/schemas/sitemap/0.9'}
try:
root = ET.parse('sitemap.xml').getroot()
except (ET.ParseError, OSError) as exc:
sys.exit(f'Could not parse sitemap: {exc}')
locations = root.findall('./sm:url/sm:loc', namespace)
if not locations:
sys.exit('No sitemap URL locations found; check the document and namespace.')
for location in locations:
url = (location.text or '').strip()
try:
parts = urlsplit(url)
valid = parts.scheme in ('http', 'https') and bool(parts.hostname)
except ValueError:
valid = False
if not valid:
sys.exit(f'Invalid or missing HTTP(S) URL: {url!r}')
print(url)
PY
A parsing error can indicate a bare ampersand or another XML syntax problem. If parsing succeeds, inspect every printed URL: parameter separators should appear as &, not &. This is a basic syntax and URL-shape check, not full sitemap schema validation or proof that the pages work.
2. Check each listed destination
For each extracted URL you own or are authorized to inspect:
- Open it in a browser and check that it reaches the intended page with the intended query parameters.
- Use the browser’s network developer tools to inspect the document request’s HTTP status and any redirects. Investigate errors and unexpected redirects rather than assuming a loaded page is the intended destination.
- Check that the final page is the version you intend to list, including its canonical and indexing settings where relevant.
Repeat the check for every listed URL; XML parsing alone does not visit them. Avoid sharing sitemap URLs that contain credentials, tokens, or other private data.
Advertisement
Common Mistakes
- Leaving a bare
&in<loc>: The XML parser may reject the document because&starts an XML entity reference. - Double-escaping:
&amp;parses to the literal text&, not the intended parameter separator. - Using
%26for a separator: This changes the URL’s meaning rather than merely escaping it for XML. - Checking only XML syntax: A sitemap can parse successfully while its URLs return errors, redirect unexpectedly, or point to the wrong pages.
- Editing the browser URL to include
&: That spelling belongs in the XML source, not in the URL you intend to visit.
Best Practices
Build and verify the destination URL first. When writing the sitemap, use an XML serializer rather than manually replacing characters. After generation, parse the resulting file and check the extracted URLs against the intended destinations.
The Sitemaps protocol documentation and W3C XML specification could not be consulted in this environment. Confirm any additional sitemap protocol requirements against those sources before deploying; the example and validation commands above have not been run here.
Conclusion
Use & to represent a query separator in sitemap XML, while keeping & in the actual destination URL. Validate the XML and visit the extracted destinations as separate checks.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.