Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
XSL Transformation With Xalan
Take your Java/XML skills to the next level by converting your XML into other formats with the very powerful Xalan XSLT engine.

| Meeting The World's Greatest Detective |

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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
$ 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):

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
$ java addressBookConverter addresses.xml addresses.xsl result.txt
').'
'

And here's what you should see:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
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.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback