Skip to content

An xml-stylesheet instruction makes browsers render an Atom feed as HTML

January 10, 2024

Browsers render an Atom feed as styled HTML instead of raw XML if the feed has an <?xml-stylesheet?> processing instruction pointing at an XSL file.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/feed.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  ...
</feed>

The XSL template transforms the feed into HTML with inline styles:

<xsl:stylesheet version="3.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:atom="http://www.w3.org/2005/Atom">
  <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />
  <xsl:template match="/">
    <html>
      <head>
        <style>
          body { font-family: sans-serif; max-width: 48rem; margin: 0 auto; }
        </style>
      </head>
      <body>
        <h1><xsl:value-of select="/atom:feed/atom:title" /></h1>
        <xsl:for-each select="/atom:feed/atom:entry">
          <a href="{atom:link/@href}">
            <xsl:value-of select="atom:title" />
          </a>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

All modern browsers support it, and those that don’t just show the raw XML.

—

Back to TIL

Next: Phoenix Component attrs take a list of examples and an exhaustive list of values

Phil-Bastian Berndt

Phil-Bastian Berndt
Tech Lead at Naymspace