xsl:calltemplate with name supplied by a parameter

2019-07-23 13:50发布

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?

标签: xslt
2条回答
Explosion°爆炸
2楼-- · 2019-07-23 14:04

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.

查看更多
再贱就再见
3楼-- · 2019-07-23 14:21

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.

查看更多
登录 后发表回答