在apply-templates模式下使用变量的值

mar*_*a90 12 xslt xslt-1.0

我想应用具有依赖于变量值的模式的模板.

<xsl:variable name="mode" select="@attribute"/>
<xsl:apply-templates mode="{$mode}"/>
Run Code Online (Sandbox Code Playgroud)

我收到样式表无法编译的错误.mode的值应该是QName,但它是"{$ mode}".

是否有可能使用依赖于变量的模式?

Mar*_*nen 7

您必须使用基于表达式的特定模式的唯一选项

<xsl:choose>
   <xsl:when test="@attribute = 'foo'">
      <xsl:apply-templates mode="bar"/>
   </xsl:when>
   <xsl:otherwise>
      <xsl:apply-templates/>
   </xsl:otherwise>
</xsl:choose>
Run Code Online (Sandbox Code Playgroud)

或与...相同xsl:if.该mode属性值本身必须是一个QName在XSLT 1.0分别在XSLT 2.0允许QName或特殊记号等#current#default".但是您无法mode在运行时计算值.


Sea*_*kin 5

模式不是属性值模板 (AVT) 的有效候选者。你根本无法做到这一点。

XSLT 2.0 规范

[定义:在指定为属性值模板的属性中,例如文字结果元素的属性,可以通过用大括号 ({}) 将表达式括起来来使用表达式]。

模式在规范中没有指定为 AVT,所以你不能这样做。