Sun Java System Application Server 

Samples Index

XML XSLT Sample Application

Extensible Stylesheet Language Transformation (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 an interpreting version (Xalan) of XSLT.

NOTE: This application is not a J2EE application nor a browser-based application. It is a Java program that is run from the command line.

Building the Application

To build the Application, follow these steps:

  1. Change your working directory to install_dir/samples/xml/apps/xslt.
  2. Enter the command asant to build the application.

This application does not require deployment.

Running the Application

This application can perform many different tasks, depending on the command-line parameter passed to it through asant. To run the application, change your working directory to install_dir/samples/xml/apps/xslt, and enter the command listed for the task. The application can do any of the following

  1. Write Out a DOM as an XML File.
  2. Write Out a Subtree of the DOM as an XML File
  3. Generate XML from an Arbitrary Data Structure using Simple Parser.
  4. Generate XML from an Arbitrary Data Structure Using Parser which could Generate SAX Events.
  5. Basic Transform
  6. Trim Whitespace
  7. Transform to Remove Newlines and Whitespace After Headings
  8. Process Remaining Structure Elements
  9. Process Inline Elements
  10. Filter Chain

Write 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>

Write 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>

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

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>

Basic Transform

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>

Trim 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>

Transforming XML Data with XSLT

This transform removes newline characters and white space after 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>

Process 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>

Process Inline 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>

Chain Filters chain

In this transform, the 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 can't compile the application, make sure you have asant in your PATH environment variable.

If you don't receive the required results, or you receive an error, make sure you have XML data files under install_dir/samples/xml/apps/data directory.



Copyright © 2006 Sun Microsystems, Inc. All rights reserved.