XSLT Processing in Java
Earlier I wrote about the system that I’ve put in place to help automate the reporting of analytics about some of the services under development as part of the Aus-e-Stage project. In that post I mentioned that I was using an XSL Transformation to convert the XML format used for reporting into HTML for display.
The basic report format is very simple and is made up of tags that describe the content of a report. You can see a screen capture of the report below:
The XML file contains tags that describe the report as well as a series of section tags. These tags define the various sections in the report. Each section contains a heading, some content and / or a chart. For simplicity the content and description tags have HTML content enclosed in CDATA tags. This is for two reasons.
- I use the Java XML DOM classes to construct XML and keeping track of HTML tags gets complicated very quickly.
- Using CDATA sections makes the XSLT much simpler.
The XSLT that is currently in use can be downloaded from the Aus-e-Stage site hosted on Google Code.
The XSLT is applied to the report XML dynamically as part of the Aus-e-Stage website using a Java Servlet. I was rather surprised with how easy it was to apply a transformation. Example code is as follows:
// import the XML / XSLT processing packages
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
// import additional packages
import java.io.*;
// declare XML & XSLT related variables
TransformerFactory factory; // transformer factory
Transformer transformer; // transformer object to do the transforming
Source xslSource; // object to hold the XSL
Source xmlSource; // object to hold the XML
File xslFile; // file object for the XSL
File xmlFile; // file object for the XML
StringWriter writer; // object to receive output of transformation
StreamResult result; // object to receive output of transformation
// declare other private variables
String htmlOutput;
String xsltPath = "path to xslt file"
String xmlPath = "path to xml file"
// open the XSL file
try {
xslFile = new File(xsltPath);
// check to ensure we can read this file
if(xslFile.canRead() == false) {
// do something about this error
}
} catch (SecurityException ex) {
// do something about this error
}
// open the XML file
try {
xmlFile = new File(xmlPath);
// check to ensure we can read this file
if(xmlFile.canRead() == false) {
// do something about this error
}
} catch (SecurityException ex) {
// do something about this error
}
// set up the source objects for the transform
try {
// get a transformer factory
factory = TransformerFactory.newInstance();
// load the XSLT source
xslSource = new StreamSource(xslFile);
// load the XML
xmlSource = new StreamSource(xmlFile);
// get a transformer using the XSL as a source of instructions
transformer = factory.newTransformer(xslSource);
// get objects to handle the output of the transformation
writer = new StringWriter();
result = new StreamResult(writer);
// do the transformation
transformer.transform(xmlSource, result);
// get the results
htmlOutput = writer.toString();
} catch (javax.xml.transform.TransformerException ex) {
// do something about this error
}
// hack to fix wrong entities in the html output
htmlOutput = htmlOutput.replace("<", "<");
htmlOutput = htmlOutput.replace(">", ">");
As I mentioned before sections of the report where HTML code is used is enclosed in CDATA tags. When these are processed by the XSLT processor the < and > characters are converted to entities. This breaks the output. This is what the last two lines in the code are designed to fix. It is the downside to taking the easy approach and using the CDATA sections.
So far the code has worked really well and it has provided us with a template for how we plan on managing other aspects of the systems that we’re developing such as the display of results of analysis of audience feedback.



Comments are closed.