Working with XML Lesson 7 of 9
XSLT — Transforming XML into HTML
Transform XML into HTML with XSLT — templates, xsl:for-each, xsl:value-of, xsl:sort and xsl:if — and run a transformation live in the browser with XSLTProcessor.
XSLT (Extensible Stylesheet Language Transformations) is a language for converting XML into another format, most often HTML. Think of it as a template: "for each product in the XML, output a table row with its name and price".
A stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table>
<tr><th>Product</th><th>Price</th></tr>
<xsl:for-each select="store/product">
<xsl:sort select="price" data-type="number" order="descending"/>
<tr>
<td><xsl:value-of select="name"/></td>
<td>₱<xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
xsl:template match="/"starts at the root of the XML document.xsl:for-each select="..."loops over elements chosen with XPath.xsl:value-of select="name"outputs the text of thenameelement.xsl:sortorders the loop.
Run it live
<div id="result" style="font-family:sans-serif"></div>
<script>
const xmlText = `<store>
<product><name>Pandesal</name><price>25</price><stock>40</stock></product>
<product><name>Kape</name><price>8</price><stock>200</stock></product>
<product><name>Softdrink</name><price>75</price><stock>24</stock></product>
<product><name>Chips</name><price>15</price><stock>0</stock></product>
</store>`;
const xslText = `<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<h3>Price List (most expensive first)</h3>
<table border="1" cellpadding="6" style="border-collapse:collapse">
<tr><th>Product</th><th>Price</th><th>Status</th></tr>
<xsl:for-each select="store/product">
<xsl:sort select="price" data-type="number" order="descending"/>
<tr>
<td><xsl:value-of select="name"/></td>
<td>₱<xsl:value-of select="price"/></td>
<td>
<xsl:if test="stock = 0"><b style="color:crimson">Sold out</b></xsl:if>
<xsl:if test="stock > 0">Available</xsl:if>
</td>
</tr>
</xsl:for-each>
</table>
<p>Total products: <xsl:value-of select="count(store/product)"/></p>
</xsl:template>
</xsl:stylesheet>`;
const parse = (t) => new DOMParser().parseFromString(t, "application/xml");
const proc = new XSLTProcessor();
proc.importStylesheet(parse(xslText));
const fragment = proc.transformToFragment(parse(xmlText), document);
document.getElementById("result").append(fragment);
</script>
Change order="descending" to ascending, or add a product to the XML, and run it again.
Conditions: xsl:if and xsl:choose
<xsl:choose>
<xsl:when test="grade >= 90">Outstanding</xsl:when>
<xsl:when test="grade >= 75">Passed</xsl:when>
<xsl:otherwise>Failed</xsl:otherwise>
</xsl:choose>
Inside XSLT (which is XML), < and > in conditions must be written as &lt; and &gt;.
Linking a stylesheet to an XML file
Add this line after the XML declaration, and some browsers and tools apply the stylesheet when the XML file is opened:
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>
Many websites use this trick to make their sitemap.xml look like a readable web page instead of raw XML.
Where XSLT is used
- Turning XML data into HTML pages, PDFs (with XSL-FO) or other XML formats.
- Converting between the XML formats of two business systems.
- Publishing documentation and books written in XML formats like DocBook.
For new web apps, developers more often load data as JSON and render it with JavaScript or a framework. XSLT remains common in enterprise systems and document publishing.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04