About us
Products
Services
Articles
Contact us

Wrapper Transformer

This example is based on SmartTransformer, but it creates an HTML file as output. The SAX events are ignored during the mixed parsing. The markup that results from the transformation of the DOM trees is outputted between a header and a footer, which are taken from an HTML wrapper.

Download Java source code, compiled classes and full documentation.

We have the following WrapperTransformer.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <table>
        <record>
            <painter>Matisse, Henri</painter>
            <title>La Musique</title>
            <year>1939</year>
            <size>115.2 x 115.2 cm</size>
        </record>
        <record>
            <painter>Monet, Claude</painter>
            <title>The Artist's Garden at Vetheuil</title>
            <year>1881</year>
            <size>100 x 80 cm</size>
        </record>
        <record>
            <painter>Monet, Claude</painter>
            <title>Poplars along the River Epte, Autumn</title>
            <year>1891</year>
            <size>100 x 65 cm</size>
        </record>
        <record>
            <painter>Monet, Claude</painter>
            <title>Meule, Soleil Couchant</title>
            <year>1891</year>
            <size>73.3 x 92.6 cm</size>
        </record>
        <record>
            <painter>Turner, Joseph Mallord William</painter>
            <title>Snowstorm</title>
            <year>1842</year>
            <size>91.5 x 122 cm</size>
        </record>
    </table>

We want to transform the XML table into an HTML table using WrapperTransformer.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="record">
            <tr>
                <td><xsl:value-of select="painter"/></td>
                <td><xsl:value-of select="title"/></td>
                <td><xsl:value-of select="year"/></td>
                <td><xsl:value-of select="size"/></td>
            </tr>
        </xsl:template>

    </xsl:stylesheet>

The $$$ string from the following wrapper.html file will be replaced with the content of the table.

    <html>
    <head>
    <title>Table</title>
    </head>
    <body>
    <table cellpadding="5" cellspacing="5" border="1">
    $$$
    </table>
    </body>
    </html>

Execute this command:

    java com.devsphere.examples.xml.saxdomix.WrapperTransformer
    -in WrapperTransformer.xml -out WrapperTransformer.html
    -xsl WrapperTransformer.xsl -method xhtml -omitdecl
    -wrapper wrapper.html -variable $$$

The resulted WrapperTransformer.html file looks like this:

    <html>
    <head>
    <title>Table</title>
    </head>
    <body>
    <table cellpadding="5" cellspacing="5" border="1">
    <tr>
        <td>Matisse, Henri</td>
        <td>La Musique</td>
        <td>1939</td>
        <td>115.2 x 115.2 cm</td>
    </tr>
    <tr>
        <td>Monet, Claude</td>
        <td>The Artist's Garden at Vetheuil</td>
        <td>1881</td>
        <td>100 x 80 cm</td>
    </tr>
    <tr>
        <td>Monet, Claude</td>
        <td>Poplars along the River Epte, Autumn</td>
        <td>1891</td>
        <td>100 x 65 cm</td>
    </tr>
    <tr>
        <td>Monet, Claude</td>
        <td>Meule, Soleil Couchant</td>
        <td>1891</td>
        <td>73.3 x 92.6 cm</td>
    </tr>
    <tr>
        <td>Turner, Joseph Mallord William</td>
        <td>Snowstorm</td>
        <td>1842</td>
        <td>91.5 x 122 cm</td>
    </tr>

    </table>
    </body>
    </html>

Copyright © 2000-2020 Devsphere

About us
Products
Services
Articles
Contact us