How to produce a HTML output using XML and XSL transformation


In this post, I will show how to produce a HTML file using XML and XSL transformation;

XSL files are used to get (iterate) the required data from XML files.  Lets examine the following web application’s source code;

XML file; bank.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<?xml-stylesheet type=”text/xsl” href=”bank.xsl”?>
<bnk:Bank
xmlns:bnk=”http://www.tunatore.wordpress.com/bank&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://www.tunatore.wordpress.com/bank bank.xsd”>

    <bnk:BankName>IT Bank of Turkey</bnk:BankName>
    <bnk:Branch>
        <bnk:branchID>11111</bnk:branchID>
        <bnk:branchName>Istanbul Branch</bnk:branchName>
        <bnk:branchLocation>Istanbul</bnk:branchLocation>
    </bnk:Branch>

    <bnk:Branch>
        <bnk:branchID>12345</bnk:branchID>
        <bnk:branchName>New York Branch</bnk:branchName>
        <bnk:branchLocation>New York</bnk:branchLocation>
    </bnk:Branch>

    <bnk:Branch>
        <bnk:branchID>222222</bnk:branchID>
        <bnk:branchName>Miami Branch</bnk:branchName>
        <bnk:branchLocation>Miami</bnk:branchLocation>
    </bnk:Branch>

    <bnk:Employee>
        <bnk:employeeID>12345678</bnk:employeeID>
        <bnk:name>Tuna</bnk:name>
        <bnk:surname>TORE</bnk:surname>
        <bnk:profession>IOC Advocate, Abstract Developer</bnk:profession>
    </bnk:Employee>

    <bnk:Employee>
        <bnk:employeeID>987654321</bnk:employeeID>
        <bnk:name>James</bnk:name>
        <bnk:surname>Gosling</bnk:surname>
        <bnk:profession>Java GURU</bnk:profession>
    </bnk:Employee>

    <bnk:Employee>
        <bnk:employeeID>11111111</bnk:employeeID>
        <bnk:name>Linus</bnk:name>
        <bnk:surname>Torvalds</bnk:surname>
        <bnk:profession>Linux GURU</bnk:profession>
    </bnk:Employee>

</bnk:Bank>

XSL file bank.xsl

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
  <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform&#8221;
   xmlns:bnk=”http://www.tunatore.wordpress.com/bank&#8221; exclude-result-prefixes=”xhtml” >
  <xsl:output indent=”yes” method=”html” doctype-public=”-//W3C//DTD XHTML 1.0 Transitional//EN”
    doctype-system=”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”/&gt;
  <xsl:template match=”bnk:Bank”>
    <html xmlns=”http://www.w3.org/1999/xhtml”&gt;
    <head>
    <title>COMPANY</title>
    </head>
    <body bgcolor=”#CEF6CE”>
        <strong><h1><xsl:value-of select=”bnk:BankName”/></h1></strong>
         <table>
            <tr>
               <td>
               <h2>BRANCHES</h2>
               <table width=”500px” cellpadding=”0″ cellspacing=”0″ border=”1″>
                <tr>
                  <th>branchID</th>
                  <th>branchName</th>
                  <th>branchLocation</th>
                </tr>
                <xsl:for-each select=”//bnk:Branch”>
                  <tr>
                    <td><xsl:value-of select=”bnk:branchID”/></td>
                    <td><xsl:value-of select=”bnk:branchName”/></td>
                    <td><xsl:value-of select=”bnk:branchLocation”/></td>
                  </tr>
                </xsl:for-each>
                </table>
                </td>
             </tr>
             <tr>
               <td width=”500px”>
               <h2>EMPLOYEES</h2>
               <table width=”500px” cellpadding=”0″ cellspacing=”0″ border=”1″>
                <tr>
                  <th>employeeID</th>
                  <th>name</th>
                  <th>surname</th>
                  <th>profession</th>
                </tr>
                <xsl:for-each select=”//bnk:Employee”>
                  <tr>
                    <td><xsl:value-of select=”bnk:employeeID”/></td>
                    <td><xsl:value-of select=”bnk:name”/></td>
                    <td><xsl:value-of select=”bnk:surname”/></td>
                    <td><xsl:value-of select=”bnk:profession”/></td>
                  </tr>
                </xsl:for-each>
                </table>
                </td>
             </tr>
      </table>
    </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Servlet class: TransformXML.java

package xmlxslwebexample;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.File;
import java.io.PrintWriter;

/**
 *
 * @author tunatore
 */
public class TransformXML extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType(“text/html;charset=UTF-8”);
        PrintWriter out = response.getWriter();
        try {

            //getting XML and XSL files
            File sourceFile = new File(TransformXML.class.getResource(“bank.xml”).getFile().replace(“%20″, ” “));
            File xsltFile = new File(TransformXML.class.getResource(“bank.xsl”).getFile().replace(“%20″, ” “));

            Source xsltSource = new StreamSource(xsltFile);
            Source xmlSource = new StreamSource(sourceFile);

            TransformerFactory factory = TransformerFactory.newInstance();            
            Transformer transformer = factory.newTransformer(xsltSource);
            //producing html output by using XML and XSL transformation
            transformer.transform(xmlSource, new StreamResult(out));

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}

Jsp file: index.jsp

<%–
    Document   : index
    Created on : May 24, 2011, 1:04:01 AM
    Author     : tunatore
–%>

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
   “http://www.w3.org/TR/html4/loose.dtd”&gt;

<html>
    <head>
        <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
        <title>JSP Page</title>
    </head>
    <body>
        <jsp:forward page=”/TransformXML”></jsp:forward>
    </body>
</html>

Sample output:

 

Download Source

 

Advertisements

2 thoughts on “How to produce a HTML output using XML and XSL transformation

  1. Am really sorry i mess up your name Tunatore.
    My names’ Manuel, jst wanted to add, although this post was updated on May, 2011, this has helped me immensely understand the xsl with java & jsp processing.

    thanks so much…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s