XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Stylesheet functions

stylesheet functions

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)

Named instruction blocks resembling functions, or rather procedures, can be implemented with named templates. Any template rule can call them repeatedly at the XSLT level using xsl:call-template. XPath expressions cannot call such functions, however. This limitation was quickly recognised, prompting external language extensions.

Many EXSLT [EXSLT provides modular, open-source extensions to the XSLT 1.0 core, supported by most processors] extension functions are also named templates. True XPath-callable EXSLT functions can also be defined, using extension instruction exsl:function. Their structure resembles a template body whose result becomes the function’s return value. That describes XSLT 1.0.

XSLT 2.0: Built-in function declarations

XSLT 2.0 adopts this principle of declaring functions through a dedicated instruction as xsl:function in its core.

Each function has a name with a namespace, can accept parameters and is callable in XPath expressions of its defining stylesheet. Because its lifetime is tied to stylesheet execution, it is called a stylesheet function.

This simple example works with any source document, producing its output independently: an ounce-to-gram conversion table.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:bsp="http://beispielnamensraum">
  <xsl:function name="bsp:unze-gramm">
    <xsl:param name="gewicht"/>
    <xsl:sequence select="$gewicht * 28.349"/>
  </xsl:function>
  <xsl:template match="/">
    <html>
      <head>
        <title>Gewichtstabelle Unze – Gramm</title>
      </head>
      <body>
        <h1>Gewichtstabelle Unze – Gramm</h1>
        <table border="1">
          <tr><th>Unze</th><th>Gramm</th></tr>
          <xsl:for-each select="(1 to 20)">
            <tr>
              <td><xsl:value-of select="."/></td>
              <td><xsl:value-of select="bsp:unze-gramm(.)"/></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

How the example works

Gram values are computed in a custom function. A stylesheet function needs a qualified name; its namespace is declared on the stylesheet element. It must not be the XSL namespace, but is otherwise arbitrary. The declared prefix binds the namespace to the function name:

<xsl:function name="bsp:unze-gramm">
  <xsl:param name="gewicht"/>
  <xsl:sequence select="$gewicht * 28.349"/>
</xsl:function>

The function receives a value, passes it into its body and processes it. The value is supplied on calling the function by its QName in an XPath expression:

<xsl:value-of select="bsp:unze-gramm(.)"/>

Here the function receives an integer, the current item of the surrounding xsl:for-each- instruction. It processes integers 1 to 20, generated with the range operator to:

<xsl:for-each select="(1 to 20)">
  <!-- Current Item ist Ganzzahl --> 
  ...
</xsl:for-each>

This simulates an XSLT counting loop like FOR loops in other languages.

The function parameter

It uses an ordinary xsl:param element, but one detail distinguishes a function parameter from an ordinary parameter.

A function parameter must be empty. Since a value must be supplied, no default is needed. The instruction must therefore be empty and cannot have a select attribute.

For the same reason, attribute required is forbidden, as is tunnel. An explicit value is always required, and the function cannot access the tunnel stream.

The same applies inside the function: only its parameters can be referenced, not tunnel parameters or those in the calling template’s scope.

Global variables and parameters are accessible

Functions can access global parameters and variables. Here, two conversion factors are global variables. The function returns two values: “Ounce” and “Troy-Ounce”:

<!-- globale Variablen: -->
<xsl:variable name="ounce-faktor">28.349</xsl:variable>
<xsl:variable name="troy-faktor">31.103</xsl:variable>
<xsl:function name="bsp:gramm-unze">
  <xsl:param name="gewicht"/>
  <xsl:variable name="gramm" select="$gewicht * 10"/>
  <xsl:variable name="gramm-ounce" select="format-number($gramm div $ounce-faktor,'#0.00')"/>
  <xsl:variable name="gramm-troy" select="format-number($gramm div $troy-fak¬tor,'#0.00')"/>
  <xsl:sequence select="($gramm-ounce, '|', $gramm-troy)"/>
</xsl:function>

Code example: kap03/3.06/unze-gramm.xsl (excerpt).

The global declaration’s position relative to the function does not matter; the following-sibling scope rule does not apply. At function-call time, which is what counts, a global variable is always initialised.

Self-reference: Recursive stylesheet functions

Stylesheet functions may be recursive, providing the same capabilities as recursive named templates.

Here is a small demonstration computing an integer’s factorial:

<xsl:function name="bsp:fakultaet">
  <xsl:param name="zahl"/>
  <xsl:choose>
    <xsl:when test="$zahl=1">
      <xsl:sequence select="1"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:sequence select="$zahl * bsp:fakultaet($zahl – 1)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

Code example: kap03/3.06/fakultaet.xsl (excerpt).

The function forms a multiplication chain of the form n*(n-1)*(n-2)...*(n-(n-1)*1. It checks whether the input is 1, in which case xsl:sequence returns 1. Otherwise, the input is multiplied by a recursive call’s result, passing the input minus 1. Eventually a function instance receives 1, and multiplication resolves back up the chain.

   

back to top