The previous article in this series explained how to use XSLT to generate a list of ZwiftBooks’ top sellers. In the example, after a manual transformation we ended up with a web page dynamically generated from our XML booklist. Now let’s take the process one step further and explore how to generate the HTML dynamically from our XML by using a Java servlet whenever someone visits our web site. Then we’ll look at how server-based transformations can protect us from the impact of changing data requirements—using XSLT to convert easily between a variety of XML data formats.
Client-Side Versus Server-Side Transforms
One of the decisions that you must make when setting up a web site for XSLT-based dynamic content generation is whether to execute the transform on the client or on the server. Both methods offer advantages and disadvantages.
In client-side transforms, both the XML data and the XSLT are sent to the browser. The browser executes the transform and renders the resultant HTML. Having the client do the transform offers two advantages:
- Server configuration is simpler.
- The server can respond to requests quickly because the processing burden is pushed to the client.
However, there are two significant downsides to client processing:
- Not all browsers are capable of executing an XSLT transform.
- Privacy issues may be involved in delivering your XML to the client.
Remember, all the elements and attributes are sent—even those that don’t appear in the final HTML display.
In server-side transforms, execution occurs on the server, and only HTML is delivered to the browser. Server-side transformation requires integration with a programming language to execute the transform. While this requirement increases the complexity of the server configuration and adds to server overhead, there are no browser versioning issues. Only HTML is delivered from the server, and all your data is kept private. The good news is that numerous server-side programming options are available, including Java, PHP, Perl, and .NET. Given the tradeoffs, let’s go with the server-side transform option for this example.
Figure 1 illustrates server-side transformation when a user visits the ZwiftBooks web site. After the request comes into the web server, the request is passed off to a server component that actually performs the XSLT transformation and passes the web page back to the browser.
Figure 1 Dynamic web content generation using server-based XSLT transformation.
To make server-side transformation a reality, we need a programming language to execute the transform. Fortunately, just about all the major server-side languages provide libraries that will execute XSLT transforms for you. Whether you’re running PHP, Perl, Java, or .NET, there’s a way to load your XSLT on the server and generate the HTML. The references section at the end of this article provides links to help you get started with various languages.
The following section details how a Java servlet can carry out the task.