XML technologies / XSLT / Java and XSLT / XSLT – Advanced techniques / Combining multiple stylesheets

(Excerpt from “Java und XSLT” by Eric M. Burke)

We have seen how to use template parameters, named templates and template modes to write reusable fragments resembling function calls. Combining stylesheets lets you develop libraries of reusable XSLT templates, dramatically improving productivity.

Productivity improves because programmers need not rewrite the same code for every stylesheet. Reusable code goes in one stylesheet, imported or included by others. Maintainability is another advantage. XSLT syntax can become ugly; splitting code into small modules greatly improves readability. For example, we have seen several examples using the list of German federal presidents. Since we almost always display a president’s name, the name templates should move to a separate stylesheet. The following example shows a stylesheet intended for use by other stylesheets.

 

Example: NamensFormatierung.xslt

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html"/>
   <!-- ** Zeige einen Namen, der etwa so formatiert ist: "Burke, Eric Matthew" -->
   <xsl:template match="name" mode="NachVorMittelname">
      <xsl:value-of select="nachname"/>
      <xsl:text>, </xsl:text>
      <xsl:value-of select="vorname"/>
      <xsl:for-each select="mittelname">
         <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
         <xsl:value-of select="."/>
      </xsl:for-each>
   </xsl:template>
   <!-- ** Zeige einen Namen, der etwa so formatiert ist: "Eric Matthew Burke" -->
   <xsl:template match="name" mode="VorMittelNachname">
      <xsl:value-of select="vorname"/>
      <xsl:for-each select="mittelname">
         <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
         <xsl:value-of select="."/>
      </xsl:for-each>
      <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
      <xsl:value-of select="nachname"/>
   </xsl:template>
</xsl:stylesheet>

The code in NamensFormatierung.xslt uses template modes to determine which template is instantiated. Additional stylesheets can easily be added, making the changes available to every stylesheet importing or including this one. Because this stylesheet is intended for others to use, it has no template for the root node.

Including or importing stylesheets is essential for large websites. Every document shares a navigation bar, footer and perhaps header. Separate stylesheet fragments included by others should generate these reusable elements. For example, the copyright notice is placed once in the footer, and changes immediately appear throughout the site without programming changes.

<xsl:include>

The <xsl:include> element lets one stylesheet include another. It may occur only at the top level, making <xsl:include> elements siblings of <xsl:template> in the stylesheet structure. The syntax of <xsl:include> is:

<xsl:include href="uri-referenz"/>

Including a stylesheet inserts it at the position of the <xsl:include> element. More precisely, all children of its <xsl:stylesheet> element are inserted into the document containing <xsl:include>. Many stylesheets can be included, and they may include others in turn.

Inclusion is relatively simple: the resulting stylesheet behaves exactly as if you had typed all included elements into it. Problems can arise when two overlapping template rules are included, so plan carefully to avoid conflicts. If a conflict occurs, the XSLT processor is instructed to report an error and stop processing.

<xsl:import>

Importing rather than including a stylesheet adds some intelligence: on conflicts, the importing stylesheet takes precedence over imported ones. Unlike <xsl:include>, <xsl:import> elements must precede all children of <xsl:stylesheet>, as shown here:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <!-- xsl:import muß vor anderen Top-Level-Elementen auftauchen -->
   <xsl:import href="seitenElemente.xslt"/>
   <xsl:import href="globaleKonstanten.xslt"/>
   <xsl:output method="html"/>
   <xsl:template match="/">
      <html>
         ...
      </html>
   </xsl:template>
   <!-- xsl:include kann irgendwo auftauchen, solange es ein Top-Level-Element bleibt -->
   <xsl:include href="namensFormatierung.xslt"/>
</xsl:stylesheet>

Most websites meet these needs by importing or including common fragments on every page: templates generating headers and other reusable elements. Once a stylesheet is included or imported, its templates are available as if they were directly part of the current stylesheet.

The main reason for using <xsl:import> rather than <xsl:include> is to avoid conflicts. If your stylesheet already has a template matching seitenKopfzeilen, you cannot include seitenElemente.xslt if it contains such a template too. Instead, use <xsl:import>. Your own template seitenKopfzeilen then takes precedence over imported templates named seitenKopfzeilen.

Note:
Converting all <xsl:import> elements to <xsl:include> can reveal previously unknown name conflicts.

   

 

 

 

back to top