XSL Transformation With Xalan - Meeting The World's Greatest Detective (
Page 3 of 6 )
With everything installed and configured,
it's time to get your hands dirty. First up, a simple example, just to get you
comfortable with how Xalan works. Consider the following XML file, snipped out
from my XML-encoded address book:
<?xml version="1.0"?>
<me>
<name>Sherlock Holmes</name>
<title>
World's Greatest Detective</title>
<address>221B Baker Street, London,
England</address>
<tel>123 6789</tel>
<email>sherlock@holmes.domain.com</email>
<url>http://www.method_and_madness.com/</url>
</me>
Now, let's suppose I wanted to convert this snippet into the following plaintext
file:
Contact information for "Sherlock Holmes, World's Greatest Detective"
Mailing
address: 221B Baker Street, London, England
Phone: 123 6789
Email address: sherlock@holmes.domain.com
Web
site URL: http://www.method_and_madness.com/
Here's the XSLT stylesheet to get from point A to point B:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
method="text" />
<xsl:template match="/" >
Contact information for "<xsl:value-of
select="normalize-space(me/name),
<xsl:value-of select="normalize-space(me/title)"
/>" Mailing address:
<xsl:value-of select="normalize-space(me/address)" />
Phone:
<xsl:value-of select="normalize-space(me/tel)" />
Email address: <xsl:value-of
select="normalize-space(me/email)" /> Web
site URL: <xsl:value-of select="normalize-space(me/url)"
/>
</xsl:template> </xsl:stylesheet>
Now, we've got the XML and the XSLT. All we need is something to marry the two
together.
// import required classes
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import
java.io.*;
public class addressBookConverter {
// store the names of the
files
public static String xmlFile, xslFile, resultFile = "";
// constructor
public addressBookConverter(String xmlFile, String xslFile, String
resultFile)
{
try {
// create an instance of the TransformerFactory
// this allows the developer to use an API that is independent
of
// a particular XML processor implementation.
TransformerFactory
tFactory
= TransformerFactory.newInstance();
// create a transformer
which takes the name of the stylesheet
// as an input parameter.
// this creates a Templates object which is applied to the XML
file
// in the next step
Transformer transformer = tFactory.newTransformer(new
StreamSource(xslFile));
// transform the given XML file with the Templates object
transformer.transform(new StreamSource(xmlFile), new
StreamResult(resultFile));
System.out.println("Done!");
} catch (TransformerException e)
{
System.err.println("The following error occured: " + e);
}
}
// everything starts here
public static void main (String[]
args) {
if(args.length != 3) {
System.err.println("Please
specify three parameters:n1.
The name and path to the XML file.n2. The name and
path to the XSL
file.n3. The name of the output file.");
return;
}
// assign the parameters passed as input parameters
to
// the variables defined above
xmlFile = args[0];
xslFile
= args[1];
resultFile = args[2];
addressBookConverter myFirstExample
= new addressBookConverter
(xmlFile, xslFile, resultFile);
}
}
Now compile the class:
$ javac addressBookConverter.java
{/output]
Assuming that all goes well, you
should now have a class file named
"addressBookConverter.class". Copy this class
file to your Java
CLASSPATH, and then execute it.
[output]
$ java addressBookConverter
Ummm...Houston, we have a problem. Here's what you should see (unless you're
really intelligent and spotted me setting you up):
Please specify three parameters:
1. The name and path to the XML file.
2. The
name and path to the XSL file.
3. The name of the output file.
Let's try it again:
So, if the XML and XSL files are placed in the same folder as our application,
you can use the following syntax to run the application:
$ java addressBookConverter addresses.xml addresses.xsl result.txt
And here's what you should see:
Contact information for "Sherlock Holmes, World's Greatest Detective"
Mailing
address: 221B BakerStreet, London, England
Phone: 123 6789
Email address: sherlock@holmes.domain.com
Web
site URL: http://www.method_and_madness.com/
Let's look at the code in detail.