About us
Products
Services
Articles
Contact us

Smart Transformer

This example is similar to MixedTransformer, but it knows when to request the DOM trees for transformation. SmartTransformer parses the XSL file and analyzes the match attribute of the xsl:template and xsl:key tags in order to find out the names of the elements that have associated template rules in the XSL file. This process is done before the mixed SAX-DOM parsing.

Download Java source code, compiled classes and full documentation.

We have the following SmartTransformer.xml file:

    <?xml version="1.0" encoding="utf-8"?>

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

We want to apply the transformation instructions contained by a file called SmartTransformer.xsl to each row of the table. The content of SmartTransformer.xsl is included below:

    <?xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

        <xsl:template match="row">
            <record>
                <painter><xsl:value-of select="@painter"/></painter>
                <title><xsl:value-of select="@title"/></title>
                <year><xsl:value-of select="@year"/></year>
                <size><xsl:value-of select="@size"/></size>
            </record>
        </xsl:template>

    </xsl:stylesheet>

Execute this command:

    java com.devsphere.examples.xml.saxdomix.SmartTransformer
    -xsl SmartTransformer.xsl -in SmartTransformer.xml

You'll get the following output:

    <?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>

Copyright © 2000-2020 Devsphere

About us
Products
Services
Articles
Contact us