We are still using the same XML file, but added some attributes:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="food.xsl" ?>
<!-- Edited with XML Spy v4.2 -->
<breakfast_menu>
<food category="new">
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food category="new">
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food category="traditional">
<name>French Toast</name>
<price>$4.50</price>
<description>thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
An example of XSL file with xsl:if and xsl:choose, xsl:when,
xsl:otherwise. Also uses xsl:variable
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="color">yellow</xsl:variable>
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="breakfast_menu/food[@category='new']">
<tr>
<xsl:choose>
<xsl:when test="calories = 900">
<td bgcolor="{$color}"><xsl:value-of select="*[3]"/></td>
<td><xsl:value-of select="price"/></td>
</xsl:when>
<xsl:otherwise>
<td bgcolor="lightblue"><xsl:value-of select="*[3]"/></td>
<td><xsl:value-of select="price"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
An example of XSL file with apply-templates:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="color">yellow</xsl:variable>
<xsl:template match="/breakfast_menu">
<html>
<body>
<table>
<xsl:apply-templates />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="food">
<tr><xsl:apply-templates /></tr>
</xsl:template>
<xsl:template match="name">
<td><xsl:value-of select="."/></td>
</xsl:template>
<xsl:template match="description">
<td><em><xsl:value-of select="."/></em></td>
</xsl:template>
<xsl:template match="price">
</xsl:template>
<xsl:template match="calories">
</xsl:template>
</xsl:stylesheet>
Example of xsl:call-template with a parameter.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="color">yellow</xsl:variable>
<xsl:template match="/breakfast_menu">
<html>
<body>
<table>
<xsl:call-template name="temp1">
<xsl:with-param name="size">+3</xsl:with-param>
</xsl:call-template>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="temp1">
<xsl:param name="size">+1</xsl:param>
<tr>
<th><FONT SIZE="{$size}" COLSPAN="3">Try our breakfast food!</FONT></th>
</tr>
<tr><xsl:apply-templates /></tr>
</xsl:template>
<xsl:template match="name">
<td><xsl:value-of select="."/></td>
</xsl:template>
<xsl:template match="description">
<td><em><xsl:value-of select="."/></em></td>
</xsl:template>
<xsl:template match="price">
</xsl:template>
<xsl:template match="calories">
</xsl:template>
</xsl:stylesheet>