XML
  Home arrow XML arrow Page 3 - 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 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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.

    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 1 hosted by Hostway