Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Reusable XSL Stylesheets and Templates

By Tony Marston
2005-04-20


Summary

I have heard some people complain that XSL is too verbose, and that it takes an enormous amount of code to do even the most simple things. These people obviously do not know how to structure their code into reusable modules. As I (hope) I have demonstrated in this article, the steps to creating reusable XSL code are not that much different from creating reusable code in any other programming language:

1. Divide the HTML page layout into separate zones.
2. Create a separate XSL template to process each zone.
3. Put the code for each template into a separate file. You may use a separate file for each template, or you may group several similar templates into a single file.
4. Incorporate the required templates into individual stylesheets by using the
<xsl:include>
statement. This makes the template available to the stylesheet at runtime, and has the same effect as calling a routine from an external library.
5. Use the same DETAIL stylesheet for multiple modes - search, input, update, enquiry and delete.
6. Use a common template to deal with all fields. In this way the XML file determines which HTML control is to be used, not the XSL stylesheet.
7. Get the XML file to identify the table and field names that need to be processed. In this way each screen can use a generic stylesheet instead of having its own customised version.

By using these techniques I have created a web application of over 300 screens using just 8 generic XSL stylesheets and a collection of standard XSL templates. This means that the speed at which I can create a new component which can use an existing stylesheet is greatly increased. Because each stylesheet is merely a collection of calls to standard templates then even the creation of a new stylesheet is now a minor matter. Just imagine how much extra effort would be required if each of those 300 screens had to be hand crafted!

Can you achieve the same level of reusability in YOUR application?

Tutorial Pages:
» Introduction
» The structure of an XML file
» The structure of an XSL file
» Levels of Reusability
» Summary


 | Bookmark
Related Tutorials:
» Starting with XML
» Performing Client-Side XSL Transformations
» Create a Google Sitemap for your Web Site
» XML and Scripting Languages
» Parsing Comma-Separated Values
» XML Security Suite: Increasing the Security of E-Business