XSL Transformation using Xalan and Java
(Part II)
««« Part I Part III »»»
- Introduction
In my first article, I introduced the
project, tools needed and Xalan. In this part II of the series,
I'll continue to explain the work involved in developing the translation system for XML
based files to plain text or HTML files using Xalan and Java.
- Getting things ready
The first step is to download the Xalan 1.2.2 for Java .
Xalan 1.2.2 for Java is available in compressed bits. To uncompress it, you'll need following utilities with respect to
your operating system:
http://xml.apache.org/dist/xalan-j/xalan-j_2_0_D07.zip
http://xml.apache.org/dist/xalan-j/xalan-j_2_0_D07.tar.gz
The next step is to set up the Java Environment. I recommend visiting
www.java.sun.com or
http://java.sun.com/j2se/1.3/
to learn more about installing the Java Environment and setting it up.
The documentation is available at: http://java.sun.com/j2se/1.3/
Once you have the Java Environment up and running we are ready to begin work
on the translation system. Thera are two important .jar files from the uncompressed Xalan for Java, and they are:
Xalan.jar & Xerces.jar.
The Xalan project contains various Java programs, which explain various
features like using the SAX and DOM with XML and XSLT. You can try out the
various programs with the similar setup. Also there are instructions for
setup with the Xalan Software.
For our examples currently, we need the files Xalan.jar and Xerces.jar in
the classpath of the java virtual machine and compiler. As these files
contain the required java classes for the XML Parser and the XSLT Compiler.
We shall use the XSLT language to compose our XSL Stylesheets. Using these
stylesheets we will be able to transform our documents into the required
output formats.
- Details
Internally and technically the XML data is mapped into a tree structure with
Root node and sub nodes. These nodes are then transformed to another tree
with nodes and sub nodes based on the details you give in the XSL
stylesheet.
A basic java program can be developed from the examples supplied with Xalan.
The java program SimpleTransform.java* shows how one can use XML, XSLT &
Java to do some simple transformations. We shall base our work on that
example*.
Depending on the input and output required we can instantiate various
objects of the XML Parser and XSLT Processor in our code. Then we can read
the XML file & parse it. Then using the XSLT processor create the new output and
store it, as a file or deliver the output as an output stream.
To compile the Simpletransform.java code file we can give this at the
command prompt:
javac -classpath c:\jdk\lib\Xalan.jar;c:\jdk\lib\Xerces.jar Simpletransform.java
The listing below shows the SimpleTransform.java adapted from the extracted
samples from Xalan. Please note that this program requires the Jar files to be in the classpath for the compilation.
//program Simpletransform.java
//necessary import files
import org.apache.xalan.xslt.*;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xalan.*;
//Here we can load the given XML and XSL files,
//transform the XML and store it in the output file.
public class SimpleTransform
{
public SimpleTransform(String xmlFile, String xslFile, String outFile)
throws
java.io.IOException,java.net.MalformedURLException,org.xml.sax.SAXException
{
// Load the XSLT Processor.
XSLTProcessor xsltProc = XSLTProcessorFactory.getProcessor();
xsltProc.process(new XSLTInputSource(xmlFile),
new XSLTInputSource(xslFile),
new XSLTResultTarget(outFile));
System.out.println("************* The result is in the " + outFile +" file*************");
}
//method : main is for testing the program and just creates an instanceand tests the results.
public static void main(String[] args)
{
try
{
SimpleTransform st=new
SimpleTransform("example.xml","example.xsl","example.out");
}
catch(Exception e)
{
System.out.println("Exception caught = " + e);
}
}
public SimpleTransform()
{
}
}
To execute the above program we need to compile it and generate the class
file. Then using the following command we can execute it :
Java -classpath c:\jdk\lib\Xalan.jar;c:\jdk\lib\Xerces.jar SimpleTransform
For successful operation of the above program we need to have two files created first.
The example.xml file contains the information for a person as shown below:
<Information>
<Name>
<FirstName>Vijay</FirstName>
<MiddleInitial>I</MiddleInitial>
<LastName>Kukreja</LastName>
</Name>
<Address>
<Street>88 Beacon Street</Street>
<Apartment>1</Apartment>
<City>Lawrence</City>
<State>MA</State>
</Address>
</Information>
The XML and XSL files can be created using any standard text editor and saved as a text file with extension .xml & .xsl respectively.
The example.out file containing the output is shown below:
Full Name : Vijay I Kukreja
Lives on Street : 88 Beacon Street
In Apartment Number : 1
In the beautiful City of : Lawrence
Which is situated in the State of : MA
We shall study the XSL file creation and usage in detail in the next article.
We will also discuss every line of code in detail and how the transformation takes place.
It would be explaining the mystery behind this conversion as to how the XML file got converted to the above
shown plain text file. The next article will also brief about the conversion from XML to HTML.
««« Part I Part III »»»
About the author:
Vijay Kukreja is a software developer and consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web and across heterogeneous systems.
Vijay has participated in numerous consulting projects involving Java, CORBA & XML, or a combination of the same. He frequently provides onsite Java and/or XML development at the high-tech companies located in and around Andover, MA. He has also published articles on Java Programming and JSP and Servlets in various online magazines with other consultants.
Vijay holds a Masters degree in Computer Science from the University of Pune (India) and has more than 6 years of experience in the application of computer technology to real-world problems.
The programs and names are registered trademarks of their appropriate
owners. The usage of the examples supplied by Apache and Sun are only meant
for educational and information purposes. The writer of this article & perfectxml.com does
not take any responsibility of the functionality of the above Xalan
Software. The intended use in this article is only educational and not
commercial or otherwise.
The programs and names are registered trademarks of their appropriate
owners. The usage of the examples supplied by Apache and Sun are only meant
for educational and information purposes. The writer of this article & perfectxml.com does
not take any responsibility of the functionality of the above Xalan
Software. The intended use in this article is only educational and not
commercial or otherwise.
|
|
|