XML technologies / XSLT / XSLT Cookbook / Exploiting the potential of XSLT 2.0 / Convert simple named templates to XSLT functions
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
XSLT 1.0 did not support writing XPath functions in XSLT, and named templates are an awkward substitute.
Solution
Prefer XSLT 2.0 functions to named templates when the sole task is to compute a result rather than produce serialized content. The following examples show where functions are much more convenient than named templates:
<!-- Mathematische Berechnungen -->
<xsl:function name="ckbk:factorial" as="xs:decimal">
<xsl:param name="n" as="xs:integer"/>
<xsl:sequence select="if ($n eq 0) then 1 else $n * ckbk:factorial($n - 1)"/>
</xsl:function>
<!-- Einfache Zuordnungen -->
<xsl:function name="ckbk:decodeColor" as="xs:string">
<xsl:param name="colorCode" as="xs:integer"/>
<xsl:variable name="colorLookup" select="('black','red','orange','yellow','green','blue','indigo','violet','white')"/>
<xsl:sequence select="if ($colorCode ge 0 and $colorCode lt count($colorLookup)) then $colorLookup[$colorCode] else 'no color'"/>
</xsl:function>
<!-- Stringmanipulationen -->
<xsl:function name="ckbk:reverse">
<xsl:param name="input" as="xs:string"/>
<xsl:sequence select="codepoints-to-string(reverse(string-to-codepoints($input)))"/>
</xsl:function>Discussion
Recall that named templates are an alternative to templates invoked solely through matching. Named templates behave much like procedures in transitional languages: an XSLT programmer transfers control using xsl:call-template explicitly to a named template, rather than relying on the more declarative semantics of template matching. XSLT, both 1.0 and 2.0, usefully allows these styles to be mixed by giving a template both a pattern and a name.
User-defined XSLT 2.0 functions do not replace named templates. The key question when choosing is whether you are merely computing a result or producing reusable content. A function expresses the former better; a template expresses the latter. In XSLT 2.0 Programmer's Reference, Michael Kay recommends functions when you merely select nodes and templates when you create new ones, although XSLT also allows functions for that purpose.
This function selects nodes:
<xsl:function name="getParts" as="item( )*">
<xsl:param name="startPartId" as="xs:string"/>
<xsl:param name="endPartId" as="xs:string"/>
<xsl:sequence select="//Parts/part[@partId ge $startPartId and @partId le $endPartId]"/>
</xsl:function>This function creates new nodes, though a template would probably be more appropriate:
<xsl:function name="getPartsElem" as="item( )">
<xsl:param name="startPartId" as="xs:string"/>
<xsl:param name="endPartId" as="xs:string"/>
<Parts>
<xsl:copy-of select="//Parts/part[@partId ge $startPartId and @partId le $endPartId]"/>
<Parts>
</xsl:function>
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de