Marty Trenouth
Marty Trenouth

Reputation: 3752

xsl:calltemplate with name supplied by a parameter

I would like to call a template based on an inbound parameter to an xsl stylesheet.

Using the parameter in the name attribute fails because $ is illegal in the context. Does this mean I have to use a xsl:choose to accomplish this?

Upvotes: 2

Views: 100

Answers (2)

Michael Kay
Michael Kay

Reputation: 163322

If you want to call templates selected dynamically then you can usually do it using xsl:apply-templates rather than xsl:call-template. One very general way of doing this is to change each

<xsl:template name="n">

to

<xsl:template name="n" match="xsl:template[@name='n']">

and then change your invalid

<xsl:call-template name="$x"/>

to a legitimate

<xsl:apply-templates select="document('')/*/xsl:template[@name=$x]">

And pass the context item as a parameter if necessary.

However, if we knew more about the problem you are trying to solve, we might be able to suggest a better way of solving it.

Upvotes: 3

Martin Honnen
Martin Honnen

Reputation: 167516

Unless you use an XSLT processor like the commercial version of Saxon 9 where you have an extension instruction like http://www.saxonica.com/documentation/extensions/instructions/call-template.xml you will need to use xsl:choose.

Upvotes: 1

Related Questions