xmlsitemapline-breaksxml-formatting

Sitemap XML line breaks: /n OR /r/n


I'm currently writing a function to generate a dynamic site map for my web app.

And I'm using template strings with backticks to build them, like:

let sitemapXML = `
  <urlset>
    <url>
      <loc>https://www.myproject.net/blog</loc>
      <lastmod>${mostRecent.toISOString().slice(0,19) + '+00:00'}</lastmod>
      <changefreq>daily</changefreq>
      <priority>0.8</priority>
    </url>
  </urlset>
`;

While this looks great on code, it messes with the XML syntax, since there's an empty line as the first line, and it also makes my XML response "ugly", with a bunch of extra spaces. So I'm thinking about doing:

// REMOVE ALL SPACES AND LINE BREAKS TO GET A SINGLE LINE STRING
sitemapXML = sitemapXML.replace(/" "/gm, "").replace(/\n/gm, ""); 

And use an xml-formatter to make it "pretty".

QUESTION

enter image description here

This xml-formatter lets you choose your line break characters. What should I choose? \n or \r\n? Is there a difference between the two or it doesn't matter?


Solution

  • An XML parser converts any \r\n to \n automatically, so you might as well generate \n to save it unnecessary work.

    Getting rid of all spaces seems questionable. For example it will change <a id="n"/> to <aid="n"/> which is not well-formed XML.