Difference between <xsl:value-of> and <xsl:for-each>

Here should go questions about transforming XML with XSLT and FOP.
martinb
Posts: 1

Difference between <xsl:value-of> and <xsl:for-each>

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.

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>


And this is my XSL document with the xsl:value-of 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>
                    <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>


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?
adrian
Posts: 2337

Re: Difference between <xsl:value-of> and <xsl:for-each>

Wed Jul 29, 2015 9:50 am

Hi,

If you edit XSLT in Oxygen, you can hover the mouse pointer over an instruction and see a description of what it does.
For XSLT tutorials for beginners I recommend W3Schools:
XSLT <xsl:value-of> Element
XSLT <xsl:for-each> Element

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 0 guests