XML Sitemap is rendering as plain text
1 min read

XML Sitemap is rendering as plain text

How to fix issues with Sitemap hreflang for a different locale / language

After adding <xhtml:link> to my sitemap file where I included <xhtml:link rel="alternate" hreflang="supported_language-code"> my XML sitemap file started rendering as a plain text file.

In the console window of Chrome, I saw an error as well which indicated the structure was off.

Caught TypeError: Cannot read properties of null (reading 'childNodes')at XMLDocument.ready (content.js:218)ready @ content.js:218

I had to adjust my <urlset> from this

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">

to this, thanks to this Stackoverflow answer

<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.w3.org/TR/xhtml11/xhtml11_schema.html http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd"

Strangely enough that while the XML was rendering properly, Google Search Console was displaying an issue for the sitemap

Your Sitemap or Sitemap index file does not properly declare the namespace!

The schema below fixed the Google issue (that's all we care about in 2021, right?) while the rendering issue still persists.

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">