Home arrow XML arrow Page 5 - XSL Transformation With Xalan

The Write Stuff - XML

If you've been following along, you already know how to parse XML documents using both SAX and the DOM with the Java-based Xerces XML parser. But why stop there? In this article, take your Java/XML skills to the next level by converting your XML into other formats with the very powerful Xalan XSLT engine

TABLE OF CONTENTS:
  1. XSL Transformation With Xalan
  2. The Introductions
  3. Meeting The World's Greatest Detective
  4. The Anatomy Of A Transformation
  5. The Write Stuff
  6. Still Hungry?
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 7
March 20, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Now, if I've done my job right, you should have a fairly clear idea of how to perform XSLT transformation at the command prompt. How about the doing the same in a Web environment?

Have no fear. The process is very similar, and will cause you no heartburn whatsoever. Let's take the very first example in the article and update it to work with the Web. Here's the XML:

<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="addresses.xsl"?> <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>
Let's suppose I wanted to convert this XML document into the following Web page:



The first step, obviously, is to create a new XSLT stylesheet to produce HTML, rather than ASCII, output.

<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> </head> <body> <h1>Contact information for <b><xsl:value-of select="me/name" /></b></h1> <h2>Mailing address:</h2> <xsl:value-of select="me/address" /> <h2>Phone:</h2> <xsl:value-of select="me/tel" /> <h2>Email address:</h2> <xsl:value-of select="me/email" /> <h2>Web site URL:</h2> <xsl:value-of select="me/url" /> </body> </html> </xsl:template> </xsl:stylesheet>
And here's the Java class code:

// imported java classes import org.xml.sax.SAXException; import javax.xml.transform.*; import javax.xml.transform.stream.*; import java.io.*; public class addressBookHTMLConverter { // store the names of the files public static String xmlFile = ""; // a Writer object to communicate with JSP private Writer out; // parameters for the getAssociatedStylesheet() method of the TransformerFactory class // Set them to null as they are not essential here String media = null , title = null, charset = null; // constructor public addressBookHTMLConverter(String xmlFile, Writer out) throws SAXException { try { this.out = out; // create an instance of the TransformerFactory TransformerFactory tFactory = TransformerFactory.newInstance(); // get the name of the stylesheet that has been defined in the XML file. // create a Source object for use by the upcoming newTransformer() method Source stylesheet = tFactory.getAssociatedStylesheet (new StreamSource(xmlFile),media, title, charset); // create a transformer which takes the name of the stylesheet // from the XML document transformer.transform(new StreamSource(xmlFile),new StreamResult(out)); out.flush(); } catch (TransformerException e) { throw new SAXException(e); } catch (IOException e) { throw new SAXException(e); } } }
The key difference between this example and the earlier application is the introduction of a new Writer object, which makes it possible to redirect output to the browser instead of the standard output device.

private Writer out;
The constructor also needs to be modified to work with the Writer object:

public addressBookHTMLConverter(String xmlFile, Writer out) throws SAXException { // snip }
Finally, the transform() method of the Transformer object needs to know that, this time, output must go to the Writer object and not to a file. Note the change in the second argument to the transform() method, below.

// create a transformer which takes the name of the stylesheet // from the XML document transformer.transform(new StreamSource(xmlFile),new StreamResult(out));
Note that, since I'm using a Writer object, I will have to build some error-handling routines into the class as well. It's instructive to examine them, and understand the reason for their inclusion.

You'll remember that I defined a Writer object at the top of my program; this Writer object provides a convenient way to output a character stream, either to a file or elsewhere. However, if the object does not initialize correctly, there is no way of communicating the error to the final JSP page.

The solution to the problem is simple: throw an exception. This exception can be captured by the JSP page and resolved appropriately. This is no different from the mechanism used when catching XML errors with the Xerces XML parser.

So that's the class. Now, we need something to tie the class to the Web server. Enter JSP.

<%@ page language="java" import="java.io.IOException" %> <html> <head> </head> <body> <% try { addressBookHTMLConverter myThirdExample = new addressBookHTMLConverter("/home/me/xml/addresses.xml ", out); } catch (Exception e) { out.println("Something bad happened!"); } %> </body> </html>
Now, start up your Web server, with the Tomcat engine, and browse to the JSP page. Here's what you'll see:



This example, though intended only as an illustration, should serve to demonstrate how easy it is to perform XSLT transformations in a Web environment with Xalan.

 
 
>>> More XML Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: