XML
Beginner
1 min read
Introduction to XSLT Templates
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Output method: produce HTML -->
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<!-- Template matching the root node -->
<xsl:template match="/">
<html>
<head><title>Library Catalogue</title></head>
<body>
<h1>Library Catalogue</h1>
<table border="1">
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Year</th>
<th>Price</th>
</tr>
<!-- Apply templates to all book elements -->
<xsl:apply-templates select="//book"/>
</table>
</body>
</html>
</xsl:template>
<!-- Template matching each book element -->
<xsl:template match="book">
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="year"/></td>
<td>
<xsl:choose>
<xsl:when test="price">
$<xsl:value-of select="price"/>
</xsl:when>
<xsl:otherwise>N/A</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>