Java Reference
In-Depth Information
Listing 15-5: Applying an XSL transform
import org.xml.sax.SAXException;
import org.apache.xalan.xslt.XSLTProcessorFactory;
import org.apache.xalan.xslt.XSLTInputSource;
import org.apache.xalan.xslt.XSLTResultTarget;
import org.apache.xalan.xslt.XSLTProcessor;
/**
* Sample code to apply a stylesheet to an xml document to create an
HTML page.
*/
public class SimpleXSLTransform
{
public static void main(String[] args)throws org.xml.sax.SAXException
{
XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
processor.process(new XSLTInputSource("MemberInfo.xml"),
new XSLTInputSource("MemberInfo.xsl"),
new XSLTResultTarget("MemberInfo.html"));
}
}
XSL stylesheets can be used either on the server side or on the client side. In practice, server-side
transforms work better; different browsers implement different subsets of the specification, so results
are unpredictable. The main drawback of using XSL transforms on the server is that they tend to be
resource intensive. It is worth experimenting with different XSL transform libraries, as some perform
transforms very much faster than others.
Retrieving Data from a Database as an XML Document
Of course, before you can transform it, you need to get your ResultSet and turn it into XML. The data
set used to create the search-results page of Figure 15-2 is derived from a single table. To create a
more detailed page, information must be combined from several different tables.
The tables accessed for the detail page include the Vehicles table, which holds the basic information
about each vehicle, and the Options table, which contains information about accessories and options.
As discussed in Chapter 11 , these tables are set up so that they correlate well to the forms used for
adding vehicles to the database, as well as being more convenient for searches.
A significant aspect of the way the Options table is designed is that it contains a number of columns
representing check box selections with YES/NO values, as well as a single text entry labeled "Other" on
the HTML form. When the form data is saved to the table, the data from all of these HTML form inputs is
combined into a column labeled LIST. Designing the table this way makes it easy to search for specific
YES/NO attributes without incurring extra overhead creating a text summary of the attributes.
Cross-
Reference
The design and layout of the member database used in the Web-
applications part of this topic is discussed in Chapter 11 .
Search WWH ::




Custom Search