I get the error that the stylesheet cannot be compiled. The value of
mode should be a QName, but it is "{$mode}".
Is there a possibilty to use modes dependent on variables?
No, this is not supported in any XSLT version -- 1.0, 2.0 or 3.0.
As you are trying in effect to emulate Higher Order Functions (HOF), you may use the underlying principle of FXSL to do this in XSLT 1.0.
FXSL 1.x is a library of templates written in pure XSLT 1.0 that supports/emulates HOF.
Here is a complete solution based on these principles:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net" exclude-result-prefixes="f">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<f:inc/>
<f:double/>
<xsl:variable name="vModeInc" select="document('')/*/f:inc[1]"/>
<xsl:variable name="vModeDouble" select="document('')/*/f:double[1]"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<nums>
<xsl:apply-templates select="$vModeInc">
<xsl:with-param name="pNodes" select="node()"/>
</xsl:apply-templates>
</nums>
==============
<nums>
<xsl:apply-templates select="$vModeDouble">
<xsl:with-param name="pNodes" select="node()"/>
</xsl:apply-templates>
</nums>
</xsl:template>
<xsl:template match="f:inc">
<xsl:param name="pNodes"/>
<xsl:apply-templates select="$pNodes" mode="incr"/>
</xsl:template>
<xsl:template match="f:double">
<xsl:param name="pNodes"/>
<xsl:apply-templates select="$pNodes" mode="double"/>
</xsl:template>
<xsl:template match="num" mode="incr">
<num><xsl:value-of select=".+1"/></num>
</xsl:template>
<xsl:template match="num" mode="double">
<num><xsl:value-of select=".*2"/></num>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following XML document:
<nums>
<num>01</num>
<num>02</num>
<num>03</num>
<num>04</num>
<num>05</num>
<num>06</num>
<num>07</num>
<num>08</num>
<num>09</num>
<num>10</num>
</nums>
the wanted, correct result is produced -- the nums/num
elements processed in one (each) of the two modes available, depending on the variable specified -- $vModeInc
(1 added to each value) or $vModeDouble
(each value is multiplied by two):
<nums>
<num>2</num>
<num>3</num>
<num>4</num>
<num>5</num>
<num>6</num>
<num>7</num>
<num>8</num>
<num>9</num>
<num>10</num>
<num>11</num>
</nums>
==============
<nums>
<num>2</num>
<num>4</num>
<num>6</num>
<num>8</num>
<num>10</num>
<num>12</num>
<num>14</num>
<num>16</num>
<num>18</num>
<num>20</num>
</nums>