XML
  Home arrow XML arrow Page 5 - XSL Transformation With Xalan
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XSL Transformation With Xalan
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 7
    2002-03-20

    Table of Contents:
  • XSL Transformation With Xalan
  • The Introductions
  • Meeting The World's Greatest Detective
  • The Anatomy Of A Transformation
  • The Write Stuff
  • Still Hungry?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    XSL Transformation With Xalan - The Write Stuff


    (Page 5 of 6 )

    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


     

       

    XML ARTICLES

    - 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...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway