Java Articles

Advertisement

JAXP XSLT Transformation Example

by Ram Satish

Share

In this article we see how to transform an XML document to HTML output using JAXP XSLT transformer.

JAXP is the Java API for XML Processing for processing XML data. JAXP supports XSLT transformation so we can use it to convert the XML document to other XML document or other formats like HTML.

XML Document to be Transformed

Here is the employee XML:

emp.xml:

Advertisement
<?xml version="1.0"?>
<employees>
<employee id="1">
<name>Joe</name>
<age>34</age>
</employee>
<employee id="2">
<name>Sam</name>
<age>24</age>
</employee>
<employee id="3">
<name>John</name>
<age>44</age>
</employee>
</employees>

XSLT Transformer

XSLT transforms the XML document to HTML. It creates a table with id, name and age as headers. Next, it iterates through each employee and adds them as rows to the table.

empXmlToHtml.xslt:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Employees</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Id</th>
<th>Name</th>
<th>Age</th>
</tr>
<xsl:for-each select="employees/employee">
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="age"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Steps to transform XML to HTML using JAXP

  1. Create TransformerFactory. TransformerFactory transfomerFactory = TransformerFactory.newInstance();
  2. Create Transformer. We need to pass in the XSLT that contains the transformation logic. Transformer transformer = transfomerFactory.newTransformer(new StreamSource(XsltTransformExample.class .getResourceAsStream("sample.xslt")));
  3. Create the result object. We want the xml to be transformed into an html file so the StreamResult contains an instance of File object. File empTableOutput = new File("empTable.html"); StreamResult result = new StreamResult(empTableOutput);
  4. We have the source object and we know where the transformed output should be written, no wits the time to do the actual transformation. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(XsltTransformExample.class.getResourceAsStream("sample.xml")); DOMSource source = new DOMSource(doc); transformer.transform(source, result);
JAXP - XSLT transformation
JAXP – XSLT transformation

Run the Example

XsltTransformExample:

package com.javarticles.jaxp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;

public class XsltTransformExample {
public static void main(String[] args) throws XMLStreamException,
IOException, Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(XsltTransformExample.class
.getResourceAsStream("sample.xml"));
DOMSource source = new DOMSource(doc);

// Create an instance of the TransformerFactory
TransformerFactory transfomerFactory = TransformerFactory.newInstance();

// Obtain the XSLT transformer
Transformer transformer = transfomerFactory
.newTransformer(new StreamSource(XsltTransformExample.class
.getResourceAsStream("sample.xslt")));

File empTableOutput = new File("empTable.html");
StreamResult result = new StreamResult(empTableOutput);
transformer.transform(source, result);
BufferedReader br = new BufferedReader(new FileReader(empTableOutput));
try {
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} finally {
br.close();
}
}
}

Output:

Emp Table in Html form
Emp Table in Html form

Download the source code

This was an example about JAXP XSLT transormation.

You can download the source code here: jaxpXsltTransformExample.zip

Share

Advertisement

Related

Advertisement

Latest

Advertisement