[xsl] [exsl] Global variables in functions

Subject: [xsl] [exsl] Global variables in functions
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 26 Feb 2001 11:32:52 +0000

As you are no doubt aware, variable references can't be used in the
'match' attribute on xsl:template or the 'match' or 'use' attributes
on xsl:key. I'm not sure exactly why (perhaps someone can enlighten
me?) but I guess it's to do with not having template match patterns or
key values determined dynamically and that the restriction is a good

Now, when we introduce extension functions we might have a problem.
What if we have:

<!-- global parameter for language -->
<xsl:param name="lang" select="'en'" />

<!-- function that tests whether the context node is in the language
     specified by the global parameter -->
<exsl:function name="my:in-selected-language">
   <exsl:return select="lang($lang)" />

<!-- template that matches para elements for which the
     my:in-selected-language() function returns true -->
<xsl:template match="para[my:in-selected-language()]">

In essence, this is the same as:

<xsl:template match="para[lang($lang)]">

but legal.

There are two solutions that I can see:

1. ban using global variables in functions altogether

2. ban using global variables in functions that are called from the
   'match' attribute on xsl:template or the 'match' or 'use'
   attributes of xsl:key

I'm tempted by 1. for simplicity. After all, the values of global
variables can be passed into functions if they need them, and I think
that would encourage standalone functions. Either way, it has to be a
dynamic constraint: an XSLT function can't define a variable that has
as its content a call to a template that uses a global variable, for


Jeni Tennison

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread