|
Helping ordinary people create extraordinary websites! |
Reusable XSL Stylesheets and TemplatesBy Tony Marston2005-04-20
Introduction When producing software it is not considered good practice to write the same code over and over again. Not only is this inefficient when writing the code in the first place, it is also inefficient when the time comes to make changes as the same change has to be made to every copy of that code. That assumes that you can actually locate every copy. Instead the code should be written once and in a format that makes it reusable. In that way when you want to execute the code you simply call the central version and pass it whatever parameters or arguments it needs. This technique has been practised for many years with traditional programming languages, but can it be applied to a relatively recent language such as XSL, and if so, then how? In my dynamic web application all my HTML output is produced using XSL transformations. The PHP code for each page extracts the data from the database, puts it into an XML file, then transforms it using the contents of a static XSL file. Each page may contain several different "zones", and the contents of each zone can be built using a different XSL template. Where the same zone appears in more than one page and therefore requires the same XSL template the code for that template need not be duplicated in each XSL stylesheet - it can be maintained in a single external file and incorporated into the XSL stylesheet at runtime by means of an statement. Take the following screen shots as examples: Figure 1 - a LIST screen
This layout can be used for any number of database tables - all that changes is the title, the column headings and the data area. The contents of the pagination area, menu, navigation and action bars are supplied in the XML file or as parameters to the transformation process, therefore common templates can be used in the XSL file without any modification. Figure 2 - a DETAIL screen
This layout can be used for any number of database tables - all that changes is the title and the data area. The contents of the scrolling area, menu, navigation and action bars are supplied in the XML file, therefore common templates can be used in the XSL file without any modification. Within these two different layouts there are common zones - the menu bar, title, navigation bar and action bar - which can be dealt with by common templates and thus do not require separate copies of the same code. Tutorial Pages: » Introduction » The structure of an XML file » The structure of an XSL file » Levels of Reusability » Summary |
|