XML technologies / XSLT / XSLT Cookbook / Exploiting the potential of XSLT 2.0 / Emulating object-oriented reuse and design patterns

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You want to move beyond cut-and-paste reuse and build reusable XSLT libraries.

Solution

Certainly, XSLT 2.0 is not object-oriented. Yet object orientation is as much about generic reusable code as about classes, objects, inheritance, encapsulation and the like.

Two XSLT 2.0 features support an object-oriented style: instruction xsl:next-match and tunnel parameters. The instruction xsl:next-match generalises the XSLT 1.0 instruction xsl:apply-imports. Recall that XSLT 1.0 uses xsl:apply-imports to invoke templates of lower import precedence. Instruction xsl:next-match generalises this to matching templates of lower priority in the same and importing stylesheets. This resembles a base-class method call:

<xsl:template match="author | title | subtitle | deck" priority="1">
  <a name="{generate-id( )}">
    <span class="{name( )}">
      <xsl:apply-templates/>
    </span>
  </a>
</xsl:template>
<xsl:template match="author" priority="2">
  <div>
    <span class="by">By </span>
    <xsl:next-match/>
  </div>
</xsl:template>
<xsl:template match="title" priority="2">
  <h1 class="title">
    <xsl:next-match/>
  </h1>
</xsl:template>
<xsl:template match="deck" priority="2">
  <h2 class="deck">
    <xsl:next-match/>
  </h2>
</xsl:template>
<xsl:template match="subtitle" priority="2">
  <h2 class="subtitle">
    <xsl:next-match/>
  </h2>
</xsl:template>

Another 2.0 improvement passes parameters to both xsl:next-match and xsl:apply-imports:

<xsl:next-match>
  <xsl:with-param name="indent" select="2"/>
</xsl:next-match>

Another feature is tunnel parameters, a form of dynamic binding popular in functional programming. Calls to xsl:apply-templates can pass parameters unknown to the immediately matching templates. These pass transparently through calls until reaching a template accepting them. Both the call and receiving declaration must use attribute tunnel="yes":

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!--Standardverarbeitungsregeln für doc -->
  <xsl:import href="doc.xslt"/>
  <!-- Ein eigener Parameter, der vom Autor von doc.xslt nicht vorgesehen war -->
  <xsl:param name="customParam"/>
  <xsl:template match="/">
    <!--Aufruf von Templates von doc.xslt, die customParam nicht kennen -->
    <xsl:apply-templates>
      <xsl:with-param name="customParam" select="$customParam" tunnel="yes"/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="heading1">
    <!-- Mit heading1-Elementen soll auf der Grundlage von customParam etwas Besonderes gemacht werden -->
    <xsl:param name="customParam" tunnel="yes"/>
    <!-- ... -->
  </xsl:template>
</xsl:stylesheet>

This is a major improvement: application-specific templates can be decoupled from generic templates without global variables or parameters.

Discussion

Design patterns are common in object-oriented development: proven techniques for recurring problems. Their established names and contexts improve communication among developers.

These capabilities, together with those in Modularisation and modes, implement standard patterns from an XSLT perspective. We now adapt some to XSLT.

Template Method

Define a stylesheet’s skeleton as an operation delegating steps to templates implemented by importing stylesheets. This lets others redefine transformation steps without changing its structure.

Imagine a stylesheet defining your company’s standard XML presentation for the web:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
  <xsl:output method="xhtml" indent="yes"/>
  <xsl:param name="titlePrefix" select=" '' " as="xs:string"/>
  <xsl:template match="/">
    <html>
      <head>
        <title><xsl:value-of select="concat($titlePrefix, /*/title)"/></title>
      </head>
      <body>
        <xsl:call-template name="standard-processing-sequence"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template name="standard-processing-sequence">
    <xsl:apply-templates mode="front-matter">
      <xsl:with-param name="mode" select=" 'front-matter' " tunnel="yes" as="xs:string"/>
    </xsl:apply-templates>
    <xsl:apply-templates mode="toc">
      <xsl:with-param name="mode" select=" 'toc' " tunnel="yes" as="xs:string"/>
    </xsl:apply-templates>
    <xsl:apply-templates mode="body">
      <xsl:with-param name="mode" select=" 'body' " tunnel="yes" as="xs:string"/>
    </xsl:apply-templates>
    <xsl:apply-templates mode="appendicies">
      <xsl:with-param name="mode" select=" 'appendicies' " tunnel="yes" as="xs:string"/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="/*" mode="#all">
    <xsl:param name="mode" tunnel="yes" as="xs:string"/>
    <div class="{$mode}">
      <xsl:apply-templates mode="#current"/>
    </div>
  </xsl:template>
  <!-- Hier kommen vorgegebene Templates für verschiedene Modi hin - diese können in importierenden Stylesheets überschrieben werden -->
</xsl:stylesheet>

Modes identify the main processing stages, but a tunnel parameter also carries the current mode name. This helps debug templates matching several modes. It also lets similar multimode templates implement minor behavioural differences through the mode parameter without knowing specific modes. For example, if a template applies CSS styles to output elements, prepend the mode name as a prefix, or use another generic mapping, such as a table lookup, from mode to CSS style.

Chain of Responsibility

Decouple the transformation initiator from processing templates by giving several templates a chance to handle requests. Use template matching rather than conditional logic to select the appropriate template.

Priorities make this portable because some processors may reject ambiguous template precedence. This example comes from a dynamic website project using Cocoon. HTML templates used class attributes to specify how database XML becomes dynamic HTML. Individual template details are omitted because structure matters more. Only one template matches each xhtm:td node here, but generally xsl:next-match combines the effects of several matching templates:

<xsl:template match="xhtm:td[matches(@class, '^keep-(\w+)')]" mode="template" priority="2.1"> </xsl:template>
<xsl:template match="xhtm:td[matches(@class, '^(flow|list)-(\w+)')]" mode="template" priority="2.2"> </xsl:template>
<xsl:template match="xhtm:td[matches(@class, '^repeat-(\w+)')]" mode="template" priority="2.3"> </xsl:template>
<xsl:template match="xhtm:td[matches(@class, '^download-(\w+)')]" mode="template" priority="2.4"> </xsl:template>
<xsl:template match="xhtm:td[matches(@class, '^keep-(\w+)')]" mode="template" priority="2.1"> <xsl:template>

Decorator

Extend lower-priority template behaviour by matching nodes in higher-priority templates with higher import precedence. Invoke the core behaviour with xsl:next-match.

This is the classic next-match use discussed under “Solution”, so no further example is needed.

  

 

 

 

back to top