XSLT Transformation Examples
Estimated study time: 13 minutes.
Seeing a full, working transformation end-to-end makes XSLT's pieces click together. Here are two complete examples.
Example 1: XML to an HTML Table
<!-- courses.xml -->
<courses>
<course level="beginner">
<title>Data Analytics</title>
<duration>4 Months</duration>
</course>
<course level="advanced">
<title>Data Science</title>
<duration>6 Months</duration>
</course>
</courses>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/courses">
<table>
<tr><th>Title</th><th>Duration</th></tr>
<xsl:for-each select="course">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="duration"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Example 2: Filtering with a Condition
<xsl:template match="/courses">
<ul>
<xsl:for-each select="course[@level='beginner']">
<li><xsl:value-of select="title"/></li>
</xsl:for-each>
</ul>
</xsl:template>
This only outputs courses where the level attribute equals "beginner", combining a predicate directly in the select path.
💡 Tip: Build transformations incrementally — get a template outputting plain text first, then layer in HTML tags, loops, and conditions one at a time.