ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

XML Tutorial• 6 min read

XSLT: Transforming XML into HTML and JSON

Templates, xsl:for-each, xsl:value-of, and stylesheet pipelines

XSLT (eXtensible Stylesheet Language Transformations) transforms XML source documents into HTML webpages, clean text, or other XML structures.

XSLT Processing Model

An XSLT processor reads the input XML document and an XSL stylesheet.

<xsl:template match="/">: Defines the root template transformation.

<xsl:for-each select="...">: Iterates over matching XML nodes in a loop.

<xsl:value-of select="...">: Extracts the string value of a specified element or attribute.

Transforming XML Books into an HTML Table
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
    <body>
      <h2>Book Inventory</h2>
      <table border="1">
        <tr bgcolor="#04AA6D" style="color:white">
          <th>Title</th><th>Author</th>
        </tr>
        <xsl:for-each select="catalog/book">
          <tr>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="author"/></td>
          </tr>
        </xsl:for-each>
      </table>
    </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
XSLT transforms raw data into presentation markup.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which XSLT element iterates over a set of XML nodes matching an XPath query to generate repetitive output markup?