XSLT Example Books

File : books.xml


<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="template.xsl"?>
<books>
    <book>
        <title>Java world</title>
        <isbn>12345</isbn>
        <author>sanmugam</author>
        <publisher>ramasamy book house</publisher>
        <price>99</price>
    </book>
    <book>
        <title>Java 101</title>
        <isbn>54321</isbn>
        <author>dhura singam</author>
        <publisher>ramasamy book house</publisher>
        <price>199</price>
    </book>   
</books>


File: template.xsl


<?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>
            <head>
                <title>Book details</title>
            </head>
            <body>
                <h1>Book Details</h1>
                <br/>
                <table border="5">
                    <tr>
                        <th>Book Name</th>
                        <th>Author</th>
                        <th>Price</th>
                    </tr>
                    <xsl:for-each select="books/book">
                        <tr>
                            <td><xsl:value-of select="title"/></td>
                            <td><xsl:value-of select="author"/></td>
                            <td><xsl:value-of select="price"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>


Output:

 

Book Details


Book NameAuthorPrice
Java worldsanmugam99
Java 101dhura singam199

No comments:

Post a Comment