XSL Transformation using Xalan and Java
(Part III)
««« Part II
- Introduction
In Part I of this series,
I introduced the project, tools needed & Xalan. Next, in Part II
we saw using Xalan from a Java program, how to convert XML files to plain text file. In this third and final
part III of the series, we'll closely look at the stylesheet file used to do the conversion.
We'll also learn how to convert XML document to HTML document using XSLT.
- XML to Text
Let's start by looking at "example.xsl" used to do the actual transformation:
example.xsl
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output
method="text"/>
<xsl:strip-space
elements="*"/>
<xsl:template
match="Information/Name/FirstName">
<xsl:text>Full Name :
</xsl:text>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template
match="Information/Name/MiddleInitial">
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template
match="Information/Name/LastName">
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template
match="Information/Address/Street">
<xsl:text> Lives on Street : </xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template
match="Information/Address/Apartment">
<xsl:text> In Apartment Number :
</xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template
match="Information/Address/City">
<xsl:text> In the beautiful City of :
</xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template
match="Information/Address/State">
<xsl:text> Which is situated in the
State of : </xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
The above file, example.xsl is the core to actual transformation of an XML document to a text document. All XSL files are standard XML documents.
The first line in example.xsl, simply defines the use of XSL language, it's version and namespace reference.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
Next line specifies the desired output method.
<xsl:output method="text"/>
The following line tells the transformation engine to trim all leading and trailing spaces as they are not needed in the output.
<xsl:strip-space elements="*"/>
Actual transformation is done using the template match tags, which tell the transformation engine to
locate a particular XML entry. The sub-elements under template match tags specify what action needs
to be done to those matching entries.
<xsl:template match="Information/Name/FirstName">
<xsl:text>Full Name : </xsl:text>
<xsl:value-of select="."/>
</xsl:template>
The "xsl:text" tag is used just to insert our own textual data. In this case, it'll just write "Full Name: " to the output.
The "value-of select" tag actually picks the matching value and inserts as part of result (in our case Vijay).
The next four lines insert a single space followed by the middle initial.
<xsl:template match="Information/Name/MiddleInitial">
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
</xsl:template>
For the first line on the output, we finally need LastName and a new line character, here is how we do it:
<xsl:template
match="Information/Name/LastName">
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
The newline is inserted using special characters 
. The specifications for the XSLT special character generation can be viewed at http://www.w3.org/TR/xslt#section-XML-Output-Method .
Since the next transformation lines are really similar to one we just saw, we'll not go into details. You can learn
more about XSL here at perfectxml.com's XSL section. Also, you can read
FREE full chapter from Wrox press book "XSLT Programmer's Reference" right here at perfectxml.com.
- XML to HTML
Let's now see what changes we need to make in above stylesheet to get the result in
HTML format rather than plain text. Here is our ToHTML.xsl:
ToHTML.xsl
<?xml
version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output
method="html" indent="yes"/>
<xsl:template
match="Information/Name/FirstName">
<html>
<head>
<title>
<xsl:text>User Information </xsl:text>
</title>
</head>
<body>
<table>
<tr>
<td>
<xsl:text>FirstName
</xsl:text>
</td>
<td>
<xsl:value-of
select="."/>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The only important change in the above stylesheet is:
<xsl:output
method="html" indent="yes"/>
The above line instructs XSL processor to generate result string in HTML format with indentation on. The indent="yes" option tells the transformation engine to store the HTML in proper alignment format.
Rest of XSL code is once again template and pattern match logic.
When applied on our original sample XML document, the above XSL will result in:
<html>
<head>
<title>User
Information</title>
</head>
<body>
<table>
<tr>
<td>FirstName
</td>
<td>Vijay</td>
</tr>
</table>
</body>
</html>
- Summary
In this article series, we wrote a small Java program that used Xalan to apply
XSL transformation on an XML document. The steps were really straightforward,
download and install Xalan, setup Java environment, write XML file and then finally
the XSL transformation file. In this final part of the series, we closely looked at the
XSL document that actually describes the transformation.
««« Part II
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.
|
|
|