This article describes a mechanism for doing the following: developing application business data and presentation data separately, and then combining them with a common XSLT style sheet. The application business data is formatted as XML, and the presentation data can be created using traditional tools. Additional properties enhance the presentation of data that the style sheet uses to apply the presentation details to the actual content. For the creation of the final format, no specialized programming is required because the process is done by the XSLT processor.
For the fullest understanding of this article, you should understand the basics of XML and XSLT. See the Resources section for links to tutorials and articles that you can use as an introduction to these topics. All of the examples shown here are generated by using the Apache Xerces XML Parser, version 2.0.1 and Apache Xalan XSLT processors, version 2.3.1 (see Resources for Apache XML Project).
Application Architecture
For the application architecture, I chose the N-tier architecture proposed by Java 2 Enterprise Edition (EE), as shown in Figure 1.
Figure 1. N-tier architecture
Here, I'll focus on layers 1th and 2nd. This architecture can be improved if you expect the user output to be processed by the servlet. This servlet invokes the application business logic with back-end communication. This backend can be (for example) a Enterprise JavaBean that exists on a stand-alone server in the network. You can assume that all business data is formatted as XML. It then prepares the data for presentation by using Java Server Page (JSP) and sends it back to the client, as shown in Figure 2.
Figure 2. Use JSP to prepare the data for presentation
If you convert the picture to model-view-Controller (Model-view-controller (MVC)) design mode, the servlet acts as the controller role, the application logic represents the model, and the JSP is the view element. (Admittedly, this allocation is simplified, but it shows the rationale.) )
Preparation of application data for presentation
To make the most of the benefits of using XML to create application data, you must first convert the data into a format that can be represented. The common way to do this is XSLT style sheets. There are two possible approaches in the XSL specification (see also the Extensible Stylesheet Language (extensible stylesheets Language) in resources). You can:
Use the so-called formatted object (FO)
Create a simple XSLT style sheet
FO typically creates binary output formats, but has not yet been widely used (even if they are more specific to editing presentation data). In addition, most of the common XSL processors and tools do not support them. On the other hand, XSLT stylesheets are more widely used, so I'll focus on XSLT style sheets in this article.
XSLT style sheet processing structure
An XSLT style sheet always uses an XML document to act as its input. The XSL processor processes the input document according to the rules in the style sheet. This will result in a new XML output document, as shown in Figure 3.
Figure 3. Working with an XSLT style sheet for XML documents
In most cases, this structure results in a mixture of rules and representations of the data in the style sheet. I assume I have address data in XML format that needs to be converted into HTML to be represented in the browser. Listing 1 shows the XML input data. Note: The output of the XSLT style sheet processing step is another XML document. HTML is not necessarily well-formed, so this will always produce XHTML (XHTML must always be well-formed). We'll discuss the issue later.