简体   繁体   English

用户xsl:call-template,值xsl:param

[英]User xsl:call-template with value of xsl:param

I'm working on some utility xslt templates and I would like to make a template in which you can call another template based on a parameter. 我正在研究一些实用程序xslt模板,并且我想制作一个模板,在其中您可以基于参数调用另一个模板。

What I want to achieve: 我要实现的目标:

<xsl:template name="OuterTemplate.AlternatingRow">
    <xsl:param name="Position" />
    <xsl:param name="CallTemplate" />
    <xsl:if test="$Position mod 2 = 0">
        <xsl:if test="$CallTemplate != ''">
            <xsl:call-template name="$CallTemplate"></xsl:call-template>
        </xsl:if>
    </xsl:if>
</xsl:template>

But I keep getting errors as <xsl:call-template name="$CallTemplate"></xsl:call-template> isn't an allowed action. 但是我一直收到错误消息,因为<xsl:call-template name="$CallTemplate"></xsl:call-template>是不允许的操作。

Is this even possible? 这有可能吗? Couldn't find much about this when googling. 谷歌搜索时找不到太多关于此的信息。

根据XSLT规范, <xsl:call-template>name属性必须是文字qname,它不能是诸如变量引用之类的表达式。

While the accepted answer of Ian Roberts is correct, it is possible in XSLT 1.0 and in XSLT 2.0 to simulate passing a "function" (template) as a parameter to another function . 尽管Ian Roberts的公认答案是正确的,但在XSLT 1.0和XSLT 2.0中可以模拟将“函数”(模板)作为参数传递给另一个函数

This has been done in the FXSL library for functional programming with XSLT (1.0 and 2.0). 这已经在FXSL库中完成,以便使用XSLT(1.0和2.0)进行功能编程。

You can read more about the underlying, main idea of FXSL here (for XSLT 2.0): http://conferences.idealliance.org/extreme/html/2006/Novatchev01/EML2006Novatchev01.html 您可以在此处(针对XSLT 2.0)阅读有关FXSL的基本主要思想的更多信息: http ://conferences.idealliance.org/extreme/html/2006/Novatchev01/EML2006Novatchev01.html

And for XSLT 1.0 here: http://conferences.idealliance.org/extreme/html/2003/Novatchev01/EML2003Novatchev01.html 对于XSLT 1.0,请点击此处: http : //conferences.idealliance.org/extreme/html/2003/Novatchev01/EML2003Novatchev01.html

The idea is that instead of a template name, we are passing as parameter a node (say $pFun ) that is uniquely matched by a template that implements the wanted functionality. 这个想法是,我们要传递一个与实现所需功能的模板唯一匹配的节点(例如$pFun )作为参数,而不是模板名称。 Thenn in the called template the following invokes the "passed as parameter" template: 以下调用的模板中的thenn调用“作为参数传递”模板:

<xsl:apply-templates select="$pFun">
 <!-- Any necessary <xsl:with-param> here -->
</xsl:apply-templates>

XSLT的动态发送机制是xsl:apply-templates,您可以在Dimitre Novatchev的FXSL库中找到巧妙的方法来构建此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM