Java Tutorial - Java Scipt : Stylesheet Formatting Test

Java Tutorial - Java Scipt :

Stylesheet Formatting Test

XSLT is one of the reasons why XML documents are such a great intermediary format. Because XML documents are structured and carry the metadata to describe themselves, we can easily transform a proper XML document into any number of intermediate or final formats. In this example, we’ll tackle the common task of transforming a custom XML document into HTML for presentation to a Web client. Our source XML document will be the order.xml file once again. We’ll need to create a stylesheet to detail the transformations we want. This is a simple stylesheet that generates an HTML view of the order. Source for the order_to_html.xsl stylesheet:

<?xml version=”1.0”?>
<xsl:stylesheet
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0”>
<xsl:output method=”html” indent=”yes”/>
<xsl:template match=”/” >
<xsl:apply-templates />
</xsl:template>
<xsl:template match=”order”>
<html>
<head><title>Order</title></head>
<body>
<h3>My Order</h3>
<table border=”1”>
<tr>
<th>Name</th>
<th>Count</th>
<th>Color</th>
<th>Size</th>
</tr>
464 Chapter 12
<xsl:apply-templates select=”line_item” />
</table>
</body>
</html>
</xsl:template>
<xsl:template match=”line_item”>
<tr>
<td><xsl:value-of select=”@name” /></td>
<td><xsl:value-of select=”@count” /></td>
<td><xsl:value-of select=”@color” /></td>
<td><xsl:value-of select=”@size” /></td>
</tr>
<tr>
<td colspan=”4” >
<emphasis>
<xsl:apply-templates select=”notes” />
</emphasis>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>

Note that it makes use of XML namespaces. Line 2 specifies that the full XML namespace for elements starting with the xsl: namespace prefix. We’ve defined patterns to match against our order XML document elements. These elements are then used in the template delimited by the contents of each <xsl:template> element. The sample program will depend on dom4j, so be sure to have that installed. It will l oad the order.xml document, transform it with the order_to_html.xsl stylesheet, and then write the output back to order.html.
Here’s the source for xslt_test.java:

import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.dom4j.*;
import org.dom4j.io.*;
public class xslt_test {
public static void main( String args[] ) {
try {
// --- Read in order.xml. ---
SAXReader oReader = new SAXReader();
Document oDocument = oReader.read( “order.xml” );
Adding XML Power Tools 465
// --- Create the stylesheet and transformer. ---
StreamSource oStylesheetSource =
new StreamSource( new File( “order_to_html.xsl” ) );
Transformer oTransformer = TransformerFactory
.newInstance()
.newTransformer( oStylesheetSource );
// --- Transform the order XML document and
// --- output it as order.html. ---
DocumentSource oDocumentSource = new DocumentSource( oDocument );
StreamResult oResult = new StreamResult( “order.html” );
oTransformer.transform( oDocumentSource, oResult );
}
catch( Exception e ) {
e.printStackTrace();
}
}
}

To compile this on Windows, run the following:

javac –classpath .;%DOM4J_HOME%\dom4j-full.jar xslt_test.java

On Linux, run:

javac –classpath .:$DOM4J_HOME/dom4j-full.jar xslt_test

To run the example on Windows, use the following:

java –classpath .;%DOM4J_HOME%\dom4j-full.jar xslt_test

To run the example on Linux, use:

java –classpath .:$DOM4J_HOME/dom4j-full.jar xslt_test

After executing the sample program, the order.html file will be created. Open that file and compare it with the order.xml file. With a bit of examination, the transformations should be evident. We’ve only scratched the surface of XSLT’s transformation rules. The language is considerably more powerful than simple matching and templating, but more in-depth coverage is beyond the scope of this book.

There’s been a push to offload the XSLT processing to the client side. The latest browsers, such as Mozilla 1.x, support XSLT transformations. This not only saves processing time on the server, but also gives the user more control over the presentation. A user could choose to apply different stylesheets or use a client program that can operate on the source XML before applying the stylesheet for presentation. Currently, this is still not a feasible solution for most applications because the percentage of clients conforming to the full W3C XSLT standard is quite low. In more controlled environments, this can be a good architecture. We’ve witnessed the use of XSLT to create presentational HTML from an XML source. The XSLT language is quite flexible and powerful enough to be used in other, more creative ways. This example has only shown a small glimpse of XSLT’s true power. Hopefully, you’ll be compelled to learn more about this fascinating technology.