Log in
updated 11:29 AM UTC, May 4, 2016

Simple example to demonstrate XSL transformation using JAVA

  • Written by Madhu V Rao
  • Published in Core Java

Last week we saw how to generate an HTML using data in Ruby using ERB's.

Now, Lets see how to do the similar job using JAVA and XSL transformers.

Here is the data.xml:

www.ictforu.com/xml/data.xml

it has list of all the languages in the format:

 

<language>
    <name>Kannada</name>
    <region>Karnataka</region>
    <users>38M</users>
    <family>Dravidian</family>
</language>

Here is the XSLT file:

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

    <xsl:template match="/">
        <html>
            <body>
                <h1>Indian Languages details</h1>
                <table border="1">
                    <tr>
                        <th>Language</th>
                        <th>Family/Origin</th>
                        <th>No. of speakers</th>
                        <th>Region</th>
                    </tr>
		 <xsl:for-each select="languages-list/language">
                    <tr>
                        <td><xsl:value-of select="name"/></td>
                        <td><xsl:value-of select="family"/></td>
                        <td><xsl:value-of select="users"/></td>
                        <td><xsl:value-of select="region"/></td>
                    </tr>
                 </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet> 

 There are few important XSLT constructs which are used in the above XSL file

1) The <xsl:template> element is used to build templates. the match="/" defines the whole document

2) The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation.

3)The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set.

Now lets see how to write a simple code which takes in the above files and converts it into the HTML file

package JavaXSLTExample;

import javax.xml.transform.ErrorListener;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 *
 * @author M.Vasudevarao
 */
public class XSLTTest
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        if (args.length != 3)
        {
            System.err.println("give command as follows : ");
            System.err.println("XSLTTest data.xml converted.xsl converted.html");
            return;
        }
        String dataXML = args[0];
        String inputXSL = args[1];
        String outputHTML = args[2];

        XSLTTest st = new XSLTTest();
        try
        {
            st.transform(dataXML, inputXSL, outputHTML);
        }
        catch (TransformerConfigurationException e)
        {
            System.err.println("TransformerConfigurationException");
            System.err.println(e);
        }
        catch (TransformerException e)
        {
            System.err.println("TransformerException");
            System.err.println(e);
        }
    }

    public void transform(String dataXML, String inputXSL, String outputHTML)
            throws TransformerConfigurationException,
            TransformerException
    {

        TransformerFactory factory = TransformerFactory.newInstance();
        StreamSource xslStream = new StreamSource(inputXSL);
        Transformer transformer = factory.newTransformer(xslStream);
        StreamSource in = new StreamSource(dataXML);
        StreamResult out = new StreamResult(outputHTML);
        transformer.transform(in, out);
        System.out.println("The generated HTML file is:" + outputHTML);
    }
}

The output HTML looks exactly like :

http://ictforu.com/data/converted.html

check the pagesource for the HTML file created by the XSL transformation using Java.

 


Powered by Bullraider.com
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest