RE: [xsl] Dynamically calling a template

Subject: RE: [xsl] Dynamically calling a template
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 6 Feb 2007 22:43:30 -0000
> I was striving for the hypothetical (but illegal) solution of 
> <xsl:call-template name="$variable" /> (names cannot be variable).

Three suggestions:

(a) Have multiple task-oriented stylesheets each of which imports a
stylesheet containing common components. Start the transformation by
invoking a task-oriented stylesheet. The common code can call back using
<xsl:call-template name="name" /> provided that each task-oriented
stylesheet provides an implementation of "name": it doesn't have to be the
same code in each one.

(b) Use FXSL. This provides genuine higher-order programming, allowing you
to pass functions around as parameters, so you can call a function that was
supplied to you by your caller.

(c) Use the central idea behind FXSL, without actually using FXSL itself (it
has become quite a powerful monster). The central idea is to use an element
as a surrogate for a template. Instead of doing <xsl:call-template
name="$variable"/>, you do <xsl:apply-templates name="$variable"/>, where
$variable holds an element that will be matched by the template you want to
invoke (so there's a one-to-one correspondence between these elements and
their corresponding templates).

Michael Kay

Current Thread