Unformatted text preview:

Lab 2 Due Monday, June 14, 2004Extensible Style Sheet Language Transformations (XSLT)Figure 3.495-733 Internet Technologies Carnegie Mellon UniversityLab 2 Due Monday, June 14, 2004Extensible Style Sheet Language Transformations (XSLT)In this lab we will be programming in a transformation language called XSLT. XSLT isused to transform an XML document into another XML document (with a different structure). In order to write programs in XSLT, we need an XML parser (XSLT programs are XML documents) and an XSLT interpreter. The parser is called “Xerces”. The interpreter is called “Xalan” (Xalan uses Xerces). We have downloadedand installed both of these in an earlier lab. My classpath variable, when working with Xalan and servlets isD:\jwsdp-1.3\common\lib\servlet-api.jar;.;d:\jwsdp-1.3\jaxp\lib\endorsed\xalan.jar;d:\jwsdp-1.3\jaxp\lib\endorsed\xercesImpl.jarIn Part I we will test the installation. In Part II we will experiment with using XSLT from within a Java program. Finally, in Part III, you will be asked to write several XSLT programs and servlets. You are required to use the same names as used here for files and directories. Otherwise, the lab will be very difficult to grade and it will be harder for me to give you help.Part I Experimenting with XSLT 1) You will need to create a batch file so that you can type a simple command like “xalan f1.xml f2.xsl f3.wml”. This command shows an XML document being transformed by an XSLT program. The result is a WML (Wireless Markup Language) document.Place the following file (xalan.bat) in a directory called c:\batch.Contents of c:\batch\xalan.batjava org.apache.xalan.xslt.Process –IN %1 -XSL %2 -OUT %3Place the new directory, c:\batch, in the path variable so that the command interpreter knows where to look for DOS commands. This can be done on an NT machine by choosing start/settings/control panel/system/environment and changing the system variable “path” to include c:\batch.Figure 3.1 is an xml file called books.xml that contains data on books. It’s a copy of the file found on Page 70 of the XSLT Programmer’s Reference by Michael Kay.195-733 Internet Technologies Carnegie Mellon University<?xml version="1.0"?><books><book category="reference"><author>Nigel Rees</author><title>Sayings of the Century</title><price>8.95</price></book><book category="fiction"><author>Evelyn Waugh</author><title>Sword of Honour</title><price>12.99</price></book><book category="fiction"><author>Herman Melville</author><title>Moby Dick</title><price>8.99</price></book><book category="fiction"><author>J. R. R. Tolkien</author><title>The Lord of the Rings</title><price>22.99</price></book></books> Figure 3.1Figure 3.2 is an xslt program called booklist.xsl that converts the xml tree derived from Figure 3.1 into a new tree as shown in Figure 3.3. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="books"><html> <body> <h1>A list of books</h1> <table width="640"> <xsl:apply-templates/> </table> </body> </html></xsl:template><xsl:template match="book"><tr> <td> <xsl:number/> </td> <xsl:apply-templates/> </tr></xsl:template><xsl:template match="author | title | price">295-733 Internet Technologies Carnegie Mellon University<td> <xsl:value-of select="."/> </td></xsl:template></xsl:stylesheet> Figure 3.2Place the two files in a directory and make sure that xalan is working properly by running the following command. The output file should look like Figure 3.3 (result.html).xalan books.xml booklist.xsl result.htmlWhen debugging XSLT programs, it is often more helpful to view your output in an editor like Notepad than to view your output in a browser like Netscape. Look at the HTML document in Netscape only after you are satisfied with the way it looks in Notepad. 395-733 Internet Technologies Carnegie Mellon University<html><body><h1>A list of books</h1><table width="640"><tr><td>1</td><td>Nigel Rees</td><td>Sayings of the Century</td><td>8.95</td></tr><tr><td>2</td><td>Evelyn Waugh</td><td>Sword of Honour</td><td>12.99</td></tr><tr><td>3</td><td>Herman Melville</td><td>Moby Dick</td><td>8.99</td></tr><tr><td>4</td><td>J. R. R. Tolkien</td><td>The Lord of the Rings</td><td>22.99</td></tr></table></body></html> Figure 3.3495-733 Internet Technologies Carnegie Mellon UniversityPart II Running Xalan from within Java In Figure 3.4 we have a Java program that uses Xalan (and Xerces) to perform the same transformation wedid above from the command line. Later in this lab, we will use this technique to perform transformations through a servlet. Please make sure that you are able to run this program producing the file “out.html”.// ProduceHTML.java is a simple program that demonstrates how XSLT programs// can be executed from within Java.import java.io.IOException;import java.io.OutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import javax.xml.transform.Source;import javax.xml.transform.stream.StreamSource;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.Result;import javax.xml.transform.TransformerFactory;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;public class ProduceHTML { public static void main(String a[] ) { Source xmlDoc, xslDoc; Result result; try { FileInputStream xml = new FileInputStream("books.xml"); FileInputStream xsl = new FileInputStream("booklist.xsl"); FileOutputStream out = new FileOutputStream("out.html"); xmlDoc = new StreamSource(xml); xslDoc = new StreamSource(xsl); result = new StreamResult(out); TransformerFactory factory = TransformerFactory.newInstance(); Transformer trans = factory.newTransformer(xslDoc); trans.transform(xmlDoc,result); } catch(TransformerException e) { System.out.println("Transformer Probem" + e); } catch(IOException e) { System.out.println("An I/O problem"); } }} Figure 3.4595-733


View Full Document

CMU ISM 95733 - Homework

Download Homework
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?