XML to HTML tranformation using XSLT

I demonstrate xsl transformation of an xml file to html using a Transformation engine.
The standalone java class ParseXml.java has the following code:


package com.mattiz.transform;

import java.io.*;
// Import for XML
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class ParseXml {
	public static StringBuffer getContent(BufferedInputStream in, String xsl)
			throws Exception {
		try {
			TransformerFactory tFactory = null;
			Transformer transformer = null;
			tFactory = TransformerFactory.newInstance();
			transformer = tFactory.newTransformer(new StreamSource(xsl));
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			transformer.transform(new StreamSource(in), new StreamResult(baos));
			// Output content
			StringBuffer buf = new StringBuffer();
			buf.append(baos.toString());
			baos.close();
			return buf;
		} catch (Exception e) {
			System.out.println("Exception in getContent");
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String args[]) {
		BufferedInputStream xmlStream = null;
		/*
		 * Open the files and initialize streams
		 */
		try {
			// xml input file hard coded
			FileInputStream xmlFile = new FileInputStream("students.xml");
			xmlStream = new BufferedInputStream(xmlFile);
		} catch (Exception ex) {
			System.exit(-1);
		}
		String xsl = "students.xsl";// transformation xsl file hard coded
		StringBuffer output = null;

		try {
			output = getContent(xmlStream, xsl);
		} catch (Exception ex) {
			System.out.println("Exception during parsing");
		}
		try {
			// html output file hard coded

			PrintWriter out = new PrintWriter(new BufferedWriter(
					new FileWriter("students.html")));
			out.write(output.toString(), 0, output.toString().length());
			out.flush();
			out.close();
		} catch (Exception ex) {
			System.out.println("Exception during processing output stream");
		}
	}
}

The xml to be parsed, students.xml


<?xml version="1.0"?>
<root>
	<class_REPORT>
		<class_NAME>CLASS X</class_NAME>
		<entry>
			<student_NAME>ARUN</student_NAME>
			<english>20</english>
			<german>A+</german>
		</entry>
		<entry>
			<student_NAME>ANIL</student_NAME>
			<english>10</english>
			<german>C++</german>
		</entry>
		<entry>
			<student_NAME>SAMIR</student_NAME>
			<english>10</english>
			<german>A</german>
		</entry>
		<entry>
			<student_NAME>VIJAY</student_NAME>
			<english>21</english>
			<german>AB</german>
		</entry>
		<entry>
			<student_NAME>GEORGE</student_NAME>
			<english>11</english>
			<german>D</german>
		</entry>
	</class_REPORT>
	<class_REPORT>
		<class_NAME>CLASS XII</class_NAME>
		<entry>
			<student_NAME>JIM</student_NAME>
			<english>14</english>
			<german>A++</german>
		</entry>
	</class_REPORT>
</root>

The xsl file used for the transformation, students.xsl


<?xml version = "1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0">
	<xsl:template match="CLASS_REPORT">
		<table cellpadding="0" cellspacing="0" width="500" border="0">
			<tr bgcolor="iceblue">
				<td colspan="3" align="center">
					<b>
						<xsl:value-of select="CLASS_NAME" />
					</b>
				</td>
			</tr>
		</table>
		<table cellpadding="0" cellspacing="0" width="500" border="1">
			<xsl:for-each select="ENTRY">
				<xsl:if test="position() < 2">
					<tr bgcolor="cyan">
						<td width="25%">
							<xsl:text>STUDENT NAME</xsl:text>
						</td>
						<td width="25%">
							<xsl:text>ENGLISH</xsl:text>
						</td>
						<td width="25%">
							<xsl:text>GERMAN</xsl:text>
						</td>
					</tr>
				</xsl:if>
				<tr>
					<xsl:for-each select="STUDENT_NAME">
						<td width="25%">
							<xsl:if test="string(.)!='null'">
								<i>
									<xsl:value-of select="." />
								</i>
							</xsl:if>
						</td>
					</xsl:for-each>
					<xsl:for-each select="ENGLISH">
						<td width="25%">
							<xsl:if test="string(.)!='null'">
								<i>
									<xsl:value-of select="." />
								</i>
							</xsl:if>
						</td>
					</xsl:for-each>
					<xsl:for-each select="GERMAN">
						<td width="25%">
							<xsl:if test="string(.)!='null'">
								<i>
									<xsl:value-of select="." />
								</i>
							</xsl:if>
						</td>
					</xsl:for-each>
				</tr>
			</xsl:for-each>
			<tr bgcolor="pink">
				<td width="25%">TOTAL</td>
				<td width="25%">
					<xsl:value-of select="format-number(sum(ENTRY/ENGLISH),'###.00')" />
				</td>
				<td width="25%"></td>
			</tr>
		</table>
	</xsl:template>
</xsl:stylesheet>

The output html file produced by the tranformation, students.html

<?xml version="1.0" encoding="UTF-8"?>
<table cellpadding="0" cellspacing="0" width="500" border="0">
	<tr bgcolor="iceblue">
		<td colspan="3" align="center">
			<b>CLASS X</b>
		</td>
	</tr>
</table>
<table cellpadding="0" cellspacing="0" width="500" border="1">
	<tr bgcolor="cyan">
		<td width="25%">
			STUDENT NAME
</td>
		<td width="25%">
			ENGLISH
</td>
		<td width="25%">
			GERMAN
</td>
	</tr>
	<tr>
		<td width="25%">
			<i>ARUN</i>
		</td>
		<td width="25%">
			<i>20</i>
		</td>
		<td width="25%">
			<i>A+</i>
		</td>
	</tr>
	<tr>
		<td width="25%">
			<i>ANIL</i>
		</td>
		<td width="25%">
			<i>10</i>
		</td>
		<td width="25%">
			<i>C++</i>
		</td>
	</tr>
	<tr>
		<td width="25%">
			<i>SAMIR</i>
		</td>
		<td width="25%">
			<i>10</i>
		</td>
		<td width="25%">
			<i>A</i>
		</td>
	</tr>
	<tr>
		<td width="25%">
			<i>VIJAY</i>
		</td>
		<td width="25%">
			<i>21</i>
		</td>
		<td width="25%">
			<i>AB</i>
		</td>
	</tr>
	<tr>
		<td width="25%">
			<i>GEORGE</i>
		</td>
		<td width="25%">
			<i>11</i>
		</td>
		<td width="25%">
			<i>D</i>
		</td>
	</tr>
	<tr bgcolor="pink">
		<td width="25%">
			TOTAL
</td>
		<td width="25%">
			72.00
</td>
		<td width="25%" />
	</tr>
</table>
<table cellpadding="0" cellspacing="0" width="500" border="0">
	<tr bgcolor="iceblue">
		<td colspan="3" align="center">
			<b>CLASS XII</b>
		</td>
	</tr>
</table>
<table cellpadding="0" cellspacing="0" width="500" border="1">
	<tr bgcolor="cyan">
		<td width="25%">
			STUDENT NAME
</td>
		<td width="25%">
			ENGLISH
</td>
		<td width="25%">
			GERMAN
</td>
	</tr>
	<tr>
		<td width="25%">
			<i>JIM</i>
		</td>
		<td width="25%">
			<i>14</i>
		</td>
		<td width="25%">
			<i>A++</i>
		</td>
	</tr>
	<tr bgcolor="pink">
		<td width="25%">
			TOTAL
</td>
		<td width="25%">
			14.00
</td>
		<td width="25%" />
	</tr>
</table>

Run the standalone java application from the command line with “java ParseXml”

The html file produced has the following look:

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: