Sub topic 9
Anatomy of a sitemap file
This chapter assumes you can read basic XML and know what a URL is. You should also know that a sitemap is a file that lists URLs you want crawlers to discover. Here you will learn the structure of that file and the mistakes that most often make it invalid.
The three required pieces
A sitemap is an XML document with three element types:
urlsetis the root element. It wraps the whole file and declares the sitemap namespace.urlis one entry per page. It is a child ofurlset.locis the address of the page. It is a child ofurland is the only required child.
The optional lastmod element sits beside loc inside url. Later chapters cover it in detail. This chapter only shows where it goes.
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/</loc>
<lastmod>2026-11-01</lastmod>
</url>
<url>
<loc>https://www.example.com/blog/first-post</loc>
<lastmod>2026-10-15T09:30:00+00:00</lastmod>
</url>
</urlset>The xmlns attribute is the namespace. It must match the value shown exactly, or crawlers may reject the file.
Rules for the loc value
Most sitemap errors come from loc. Follow these rules:
- Use an absolute URL, including the protocol.
https://www.example.com/aboutis valid./aboutis not. - Use the same protocol and host as the sitemap's own location. A sitemap at
https://www.example.com/should not listhttp://URLs or URLs on another host. - Escape special characters. In XML,
&becomes&, and<,>,', and"have entity forms too. - Keep each URL under 2,048 characters.
- Save the file as UTF-8.
A URL such as https://www.example.com/list?page=2&sort=asc must appear in the file as follows:
<loc>https://www.example.com/list?page=2&sort=asc</loc>An unescaped & makes the whole file invalid XML. A parser then fails on that line, and the crawler may ignore every URL in the file.
Building the file safely
Writing XML by hand works for a few pages. For more, use a library that escapes values for you. This script uses Python's standard library.
import xml.etree.ElementTree as ET
NS = "http://www.sitemaps.org/schemas/sitemap/0.9"
ET.register_namespace("", NS)
def build_sitemap(pages, path):
urlset = ET.Element(f"{{{NS}}}urlset")
for loc, lastmod in pages:
url = ET.SubElement(urlset, f"{{{NS}}}url")
ET.SubElement(url, f"{{{NS}}}loc").text = loc
ET.SubElement(url, f"{{{NS}}}lastmod").text = lastmod
ET.ElementTree(urlset).write(path, encoding="utf-8", xml_declaration=True)
build_sitemap(
[
("https://www.example.com/", "2026-11-01"),
("https://www.example.com/about", "2026-09-12"),
],
"sitemap.xml",
)register_namespace("", NS) makes the output use the default namespace. Without it, ElementTree writes prefixes such as ns0:, which are valid XML but harder to read. ElementTree also escapes & in the URLs.
Common errors
| Symptom | Likely cause |
|---|---|
| "Couldn't fetch" or parse error | Unescaped `&`, or a missing closing tag |
| URLs ignored | Relative URLs, or a host that differs from the sitemap's |
| Namespace error | Missing or misspelled `xmlns` value |
| Strange characters | File not saved as UTF-8 |
Exercise
A sitemap entry contains <loc>https://www.example.com/search?q=shoes&color=red</loc>. Why is the file invalid, and how do you fix it?
Show answer
The & is not escaped, so the XML parser fails. Replace it with &: <loc>https://www.example.com/search?q=shoes&color=red</loc>.
Exercise
Which child element of url is required, and what must its value look like?
Show answer
loc is required. It must be an absolute URL with protocol and host, such as https://www.example.com/about.