Sun Java System logo
XML XSLT Sample Application

 

XML XSLT Sample Application
 

This document describes how to utilize the XML XSLT sample applications in conjunction with Sun Java (tm) System Application Server version 7.

This sample application document contains the following sections:


Overview



In this section, the XML XSLT sample applications are described.

The XML Stylesheet Language for Transformations (XSLT) defines mechanisms for addressing XML data (XPath) and for specifying transformations on the data, in order to convert it into other forms. JAXP includes two implementations of XSLT, an interpreting version (Xalan) and a compiling version (XSLTC) that lets you save pre-compiled versions of desired transformations as translets, for the most efficient runtime processing later on. The following is the list of sample programs that will be covered in this document.

    1. Writing Out a DOM as an XML File.
    2. Writing Out a Subtree of the DOM as an XML File
    3. Generating XML from an Arbitrary Data Structure using Simple Parser.
    4. Generating XML from an Arbitrary Data Structure Using Parser which could Generate SAX Events.
    5. Transforming XML Data with XSLT A Basic Program
    6. Transforming XML Data with XSLT A Program Trimming the Whitespace
    7. Transforming XML Data with XSLT  (Removing the newline characters and white space after the headings)
    8. Transforming XML Data with XSLT  (Processing the Remaining Structure Elements )
    9. Transforming XML Data with XSLT  (Process Inline (Content) Elements)
    10. Transforming from the Command Line (Compiling a Translet)
    11. Transforming from the Command Line (Running a Translet)
    12. Filter Chain (Concatenation of XSLT transformations)


Compiling and Assembling the Application



To build the entire application from scratch, follow these steps: .
  1. Go to <APPSERV_INSTALL_DIR>/samples/xml/xslt/src
  2. Execute the command asant
    The default target core will be executed to compile the application.
  3. Clean the application project area (optional).
  4. Execute the command asant clean to remove the sample application assemble and build directories.

     
Running the Sample Application

To run the sample application, do the following:

Go to <APPSERV_INSTALL_DIR>/samples/xml/xslt/src and execute any one of the following commands.:
 

1.  Writing Out a DOM as an XML File.

Command : asant run-TransformationApp02

Output:

run-TransformationApp02:
     [echo] java samples.xml.xslt.TransformationApp02 data/slideSample01.xml
     [java] <?xml version="1.0" encoding="UTF-8"?>
     [java] <!--  A SAMPLE set of slides  --><slideshow title="Sample Slide Show" date="Date of publication" author="Yours Truly">
     [java]
     [java]     <!-- TITLE SLIDE -->
     [java]     <slide type="all">
     [java]       <title>Wake up to WonderWidgets!</title>
     [java]     </slide>
     [java]
     [java]     <!-- OVERVIEW -->
     [java]     <slide type="all">
     [java]       <title>Overview</title>
     [java]       <item>Why <em>WonderWidgets</em> are great</item>
     [java]       <item/>
     [java]       <item>Who <em>buys</em> WonderWidgets</item>
     [java]     </slide>
     [java]
     [java] </slideshow>

2.  Writing Out a Subtree of the DOM as an XML File

Command : asant run-TransformationApp03

Output :

run-TransformationApp03:
     [echo] java samples.xml.xslt.TransformationApp03 data/slideSample01.xml
     [java] <?xml version="1.0" encoding="UTF-8"?>
     [java] <slide type="all">
     [java]       <title>Wake up to WonderWidgets!</title>
     [java]     </slide>
 
 

3.  Generating XML from an Arbitrary Data Structure using Simple Parser.

Command : asant run-AddressBookReader01

Output:

run-AddressBookReader01:
     [echo] java samples.xml.xslt.AddressBookReader01 data/PersonalAddressBook.ldif
     [java] nickname: Fred
     [java] email: fred@barneys.house
     [java] html: TRUE
     [java] firstname: Fred
     [java] lastname: Flinstone
     [java] work: 999-Quarry
     [java] home: 999-BedrockLane
     [java] fax: 888-Squawk
     [java] pager: 777-pager
     [java] cell: 555-cell

4. Generating XML from an Arbitrary Data Structure Using Parser which could Generate SAX Events.

Command : asant run-TransformationApp04

Output:

run-TransformationApp04:
     [echo] java samples.xml.xslt.TransformationApp04 data/PersonalAddressBook.xml
     [java] <?xml version="1.0" encoding="UTF-8"?>
     [java] <addressbook>
     [java]     <nickname>Fred</nickname>
     [java]     <email>fred@barneys.house</email>
     [java]     <html>TRUE</html>
     [java]     <firstname>Fred</firstname>
     [java]     <lastname>Flinstone</lastname>
     [java]     <work>999-Quarry</work>
     [java]     <home>999-BedrockLane</home>
     [java]     <fax>888-Squawk</fax>
     [java]     <pager>777-pager</pager>
     [java]     <cell>555-cell</cell>
     [java] </addressbook>
 

5.  Transforming XML Data with XSLT (Basic Program)

Command: asant run-Stylizer-a

Output :

run-Stylizer-a:
     [echo] java samples.xml.xslt.Stylizer data/article1a.xsl data/article1.xml
     [java] <html>
     [java] <body>
     [java]
     [java] <h1 align="center">A Sample Article</h1>
     [java]
     [java] <h1>The First Major Section
     [java]
     [java]
     [java]   </h1>
     [java] <p>This section will introduce a subsection.</p>
     [java] <h2>The Subsection Heading
     [java]
     [java]      </h2>
     [java] <p>This is the text of the subsection.
     [java]        </p>
     [java]
     [java] </body>
     [java] </html>
 

6. Transforming XML Data with XSLT  (Program Trimming the Whitespace)

Command : asant run-Stylizer-b

Output:

run-Stylizer-b:
     [echo] java samples.xml.xslt.Stylizer data/article1b.xsl data/article1.xml
     [java] <html>
     [java] <body>
     [java]
     [java] <h1 align="center">A Sample Article</h1>
     [java]
     [java] <h1>The First Major Section
     [java]      </h1>
     [java] <p>This section will introduce a subsection.</p>
     [java] <h2>The Subsection Heading
     [java]        </h2>
     [java] <p>This is the text of the subsection.
     [java]        </p>
     [java]
     [java] </body>
     [java] </html>
 
 

7.   Transforming XML Data with XSLT (Removing the newline characters and white space after the headings)

Command:  asant run-Stylizer-c

Output:

run-Stylizer-c:
     [echo] java samples.xml.xslt.Stylizer data/article1c.xsl data/article1.xml
     [java] <html>
     [java] <body>
     [java] <h1 align="center">A Sample Article</h1>
     [java] <h1>The First Major Section</h1>
     [java] <p>This section will introduce a subsection.</p>
     [java] <h2>The Subsection Heading</h2>
     [java] <p>This is the text of the subsection.</p>
     [java] </body>
     [java] </html>

8. Transforming XML Data with XSLT (Processing the Remaining Structure Elements )

Command: asant run-Stylizer-2

Output:

run-Stylizer-2:
     [echo] java samples.xml.xslt.Stylizer data/article2.xsl data/article2.xml
     [java] <html>
     [java] <body>
     [java] <h1 align="center">A Sample Article</h1>
     [java] <h1>The First Major Section</h1>
     [java] <p>This section will introduce a subsection.</p>
     [java] <h2>The Subsection Heading</h2>
     [java] <p>This is the text of the subsection.</p>
     [java] <h1>The Second Major Section</h1>
     [java] <p>This section adds a LIST and a NOTE.</p>
     [java] <p>Here is the LIST:</p>
     [java] <ol>
     [java] <li>Pears</li>
     [java] <li>Grapes</li>
     [java] </ol>
     [java] <p>And here is the NOTE:</p>
     [java] <blockquote>
     [java] <b>Note:</b>
     [java] <br>Don't forget to go to the hardware store on your way to the grocery!</blockquote>
     [java] </body>
     [java] </html>

9. Transforming XML Data with XSLT (Process Inline (Content) Elements)

Command:  asant run-Stylizer-3

Output:

run-Stylizer-3:
     [echo] java samples.xml.xslt.Stylizer data/article3.xsl data/article3.xml
     [java] <html>
     [java] <body>
     [java]
     [java] <h1 align="center">A Sample Article</h1>
     [java]
     [java] <h1>The First Major Section
     [java]      </h1>
     [java] <p>This section will introduce a subsection.</p>
     [java] <h2>The Subsection Heading
     [java]        </h2>
     [java] <p>This is the text of the subsection.
     [java]        </p>
     [java]
     [java] <h1>The Second Major Section
     [java]      </h1>
     [java] <p>This section adds a LIST and a NOTE.
     [java]      </p>
     [java] <p>Here is the LIST:
     [java]        </p>
     [java] <ol>
     [java] <li>Pears</li>
     [java] <li>Grapes</li>
     [java] </ol>
     [java] <p>And here is the NOTE:
     [java]        </p>
     [java] <blockquote>
     [java] <b>Note:</b>
     [java] <br>Don't forget to go to the hardware store on your
     [java]              way to the grocery!
     [java]        </blockquote>
     [java]
     [java] <h1>The <I>Third</I> Major Section
     [java]      </h1>
     [java] <p>In addition to the inline tag in the heading, this section
     [java]            defines the term <i>inline</i>, which literally means
     [java]            "no line break". It also adds a simple link to the main page
     [java]            for the Java platform (<a  href="http://java.sun.com">http://java.sun.com</a>),
     [java]            as well as a link to the
     [java]            <a href="http://java.sun.com/xml">XML</a> page.
     [java]      </p>
     [java]
     [java] </body>
     [java] </html>
 

10.  Transforming from the Command Line (Compile a Translet):

Command:  asant compile-translet

Output:

compile-translet:
     [echo] java org.apache.xalan.xsltc.cmdline.Compile data/article3.xsl
 

11. Transforming from the Command Line (Running a Translet)

Command:  asant run-translet

Output:

run-translet:
     [echo] java org.apache.xalan.xsltc.cmdline.Transform data/article3.xsl samples.xml.xslt.article3
     [java] <html>
     [java]   <body>
     [java]
     [java]     <h1 align="center">A Sample Article</h1>
     [java]
     [java]     <h1>The First Major Section
     [java]      </h1>
     [java]     <p>This section will introduce a subsection.</p>
     [java]     <h2>The Subsection Heading
     [java]        </h2>
     [java]     <p>This is the text of the subsection.
     [java]        </p>
     [java]
     [java]     <h1>The Second Major Section
     [java]      </h1>
     [java]     <p>This section adds a LIST and a NOTE.
     [java]      </p>
     [java]     <p>Here is the LIST:
     [java]
     [java]      </p>
     [java]     <ol>
     [java]
     [java]       <li>Pears</li>
     [java]
     [java]       <li>Grapes</li>
     [java]
     [java]     </ol>
     [java]     <p>And here is the NOTE:
     [java]
     [java]      </p>
     [java]     <blockquote>
     [java]       <b>Note:</b><br>Don't forget to go to the hardware store on your
     [java]              way to the grocery!
     [java]
     [java]     </blockquote>
     [java]
     [java]     <h1>The
     [java]       <I>Third</I> Major Section
     [java]
     [java]     </h1>
     [java]     <p>In addition to the inline tag in the heading, this section
     [java]            defines the term
     [java]       <i>inline</i>, which literally means
     [java]            "no line break". It also adds a simple link to the main page
     [java]            for the Java platform (
     [java]       <a href="http://java.sun.com">http://java.sun.com</a>),
     [java]            as well as a link to the
     [java]
     [java]       <a href="http://java.sun.com/xml">XML</a> page.
     [java]
     [java]     </p>
     [java]
     [java]   </body>
     [java] </html>
 

12.  A filter chain -- Output of  one transformation becomes the input of the next.

Command:  asant run-FilterChain

Output :

run-FilterChain:
     [echo] java samples.xml.xslt.FilterChain data/docbookToArticle.xsl data/article1c.xsl small-docbook-article.xml
     [java] <html>
     [java] <body>Title of my (Docbook) article Title of Section 1. This is a paragraph.</body>
     [java] </html>
 

Troubleshooting


  • If you re unable to compile the application, check whether you have asant in your PATH.

  • If you don't receive the required results, or you receive an error, check whether you have xml data files under APPSERV_INSTALL_DIR/samples/xml/xslt/src/data directory.
 

Copyright © 2002 Sun Microsystems, Inc. All rights reserved.
Last Updated July 12, 2002