Difference between <xsl:value-of> and <xsl:for-each>
Posted: Tue Jul 28, 2015 11:40 am
I'm learning XSLT and I've been looking into the two templates xsl:value-of and xsl:for-each, I've created an XML document that contains data for a restaurant menu; I've also created two XSL documents; one uses the xsl:value-of template; the other uses the xsl:for-each- I then applied each XSL document to the XML document and saved each result as a HTML file, here is my XML document.
This is my XSL document with the xsl:for-each template:
And this is my XSL document with the xsl:value-of template:
I've posted this because I want to learn about what each template does and why; I've noticed that the two XSL documents produce different output results when they're applied to the XML file; could anyone advise me on how I can learn more about the two templates?
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<menu>
<meal>
<mealname>Chicken Madras</mealname>
<description>Chicken pieces in a fiery sauce, with chips</description>
<price>£7.99</price>
</meal>
<meal>
<mealname>Tomato Soup</mealname>
<description>A tomato soup</description>
<price>£3.50</price>
</meal>
</menu>
This is my XSL document with the xsl:for-each template:
Code: Select all
<?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>Menu</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Mealname</th>
<th>Description</th>
<th>Price</th>
</tr>
<xsl:for-each select="menu/meal">
<tr>
<td><xsl:value-of select="mealname" /></td>
<td><xsl:value-of select="description" /></td>
<td><xsl:value-of select="price" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Code: Select all
<?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>Menu</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Mealname</th>
<th>Description</th>
<th>Price</th>
</tr>
<tr>
<td><xsl:value-of select="menu/meal/mealname"/></td>
<td><xsl:value-of select="menu/meal/description"/></td>
<td><xsl:value-of select="menu/meal/price"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>