This article explain how to use XSL Transformation to build an XHTML document. XSL Transform for build XHTML Assume we want transform following XML document

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book isbn="9781849511742" title="WordPress 3.0 jQuery" pdate="24/09/2010"/>
    <book isbn="9781849511407" title="WordPress Top Plugins" pdate="21/09/2010"/>
    <book isbn="9781847196569" title="WordPress 2.7 Complete" pdate="02/06/2009"/>
    <book isbn="9781849510080" title="WordPress 2.8 Theme Design" pdate="30/11/2009"/>
    <book isbn="9781847197382" title="WordPress 2.7 Cookbook" pdate="15/07/2009"/>
    <book isbn="9781847193599" title="WordPress Plugin Development: Beginner's Guide" pdate="16/02/2009"/>
    <book isbn="9781847198822" title="WordPress and Flash 10x Cookbook" pdate="19/04/2010"/>
    <book isbn="9781849512367" title="Joomla! 1.5 Cookbook" pdate="26/10/2010"/>
    <book isbn="9781849511803" title="Joomla! 1.5 Top Extensions Cookbook" pdate="18/10/2010"/>
    <book isbn="9781849512220" title="Building job sites with Joomla!" pdate="21/09/2010"/>
    <book isbn="9781849511704" title="Joomla! 1.5 Site Blueprints" pdate="26/05/2010"/>
    <book isbn="9781847199904" title="Joomla! 1.5: Beginner's Guide" pdate="05/03/2010"/>
    <book isbn="9781847195166" title="Joomla! 1.5x Customization: Make Your Site Adapt to Your Needs" pdate="24/08/2009"/>
</books>

An XSL Transform take on XML input and will produce output in several  formats. To produce XHTML output we must define the correct output format at the top of the document.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="xml" version="1.0" encoding="UTF-8"
        doctype-public="-//W3C//DTD XHTML 1.1//EN"
        doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
        indent="yes"/>

    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title>Books</title>
            </head>
            <body>
                <h1>Books</h1>
                <table>
                    <tr>
                        <th>isbn</th>
                        <th>title</th>
                        <th>publish date</th>
                    </tr>
                    <xsl:for-each select="books/book">
                        <tr>
                            <td><xsl:value-of select="@isbn"/></td>
                            <td><xsl:value-of select="@title"/></td>
                            <td><xsl:value-of select="@pdate"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

The transform is quite simple, and here, there is an image showing the output result. You can download source code

References


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *