Unformatted text preview:

Lab 2 Due Wednesday, February 6Extensible Style Sheet Language Transformations (XSLT)Figure 2.4Internet Technologies Carnegie Mellon UniversityLab 2 Due Wednesday, February 6Extensible 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 can access both by downloading the Java XML Pack - Winter 01 Release from http://java.sun.com/xml/downloads/javaxmlpack.html. We will make use of some of the other packages found in this bundle later in the course.In Part I, we will present the details associated with the installation of Xalan and Xerces. In Part II we will test the installation. In Part III we will experiment with usingXSLT from within a Java program. Finally, you will be asked to write several XSLT programs and a servlet. Please submit to blackboard the completed Lab 2 Activities sheet (named Lab2Submission.doc) and the Java servlet (named Courses.java). You are required to use the same names as used here for files and directories.Otherwise, the lab will be very difficult to grade. Part I Installing the Jax Pack on Windows NT1) All of the files that we need to download can be found on Sun’s web site at http://java.sun.com/xml/downloads/javaxmlpack.html.2) Go to this site and select Download (Winter ’01), choose “Accept” and then “FTP” and downloadfrom US East.3) Double click on the downloaded file and open them up in your zip/unzip software. Choose adirectory into which you would like to extract the files (I created a directory on my D: drive called jaxpackand unzipped the files to d:\jaxpack). 4) Look around. Make sure that you have the xalan.jar and xerces.jar files under the jaxp-1.2-ea1directory. Update your classpath variable so that it contains the following paths:path to xerces.jar path to xalan.jar path to jdk tools.jar the . which means the current directoryThis can be done on an NT machine by choosing start/settings/control panel/system/environment tag and then changing the user variable classpath. 1Internet Technologies Carnegie Mellon UniversityAfter completing Lab 1 and Lab 2, here is a copy of my classpath settings:D:\jaxpack\java_xml_pack-winter-01-dev\jaxp-1.2-ea1\xalan.jar;D:\jaxpack\java_xml_pack-winter-01-dev\jaxp-1.2-ea1\xerces.jar;d:\jdk1.2.2\lib\tools.jar;.;D:\jakarta-tomcat-4.0.1\common\lib\servlet.jar5) You will need to create a batch file so that you can type a simple command like “xalan f1.xml f2.xsl f3.wml”. An XML document is being transformed by an XSLT program. A WML document is being produced.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.After completing Lab 1 and Lab 2, I have my JAVA_HOME set to d:\jdk1.2.2 and my path variable set to a lot of unrelated things but including c:\batch.2Internet Technologies Carnegie Mellon UniversityPart II Experimenting with XSLTFigure 2.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.<?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 2.1Figure 2.2 is an xslt program called booklist.xsl that converts the xml tree derived from Figure 2.1 into a new tree as shown in Figure 2.3. 3Internet Technologies Carnegie Mellon University<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"><td> <xsl:value-of select="."/> </td></xsl:template></xsl:stylesheet> Figure 2.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 2.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. 4Internet 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 2.35Internet Technologies Carnegie Mellon UniversityPart III Running Xalan from within Java In Figure 2.4 we have a Java program that uses Xalan (and Xerces) to perform the same transformation wedid above


View Full Document

CMU ISM 95733 - Lab

Download Lab
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 Lab 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 Lab 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?