XSLT模板模式 - xpath评估

时间:2013-02-14 17:49:01

标签: xslt xpath

我想基于源XML的属性动态更改apply-templates模式,如下所示:

<xsl:choose>
    <xsl:when test="@myAttribute">
        <xsl:apply-templates select="." mode="@myAttribute"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:apply-templates select="." mode="someOtherMode"/>
    </xsl:otherwise>
</xsl:choose>

是否可以在mode属性中评估XPath?还有其他方法吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

不,没有办法为mode属性使用动态值。它必须是静态的。在你的情况下,我建议做这样的事情(使用名称 myNode 作为上面例子的上下文节点):

<xsl:template match="myNode[@myAttribute = 'someValue']" mode="specialHandling">
   <!-- template contents -->
</xsl:template>

<xsl:template match="myNode[@myAttribute = 'someOtherValue']" mode="specialHandling">
   <!-- template contents -->
</xsl:template>

<xsl:template match="myNode[@myAttribute = 'aThirdValue']" mode="specialHandling">
   <!-- template contents -->
</xsl:template>

<xsl:template match="myNode[not(@myAttribute)]" mode="specialHandling">
   <!-- template contents -->
</xsl:template>

然后你甚至不需要xsl:choose。你可以这样做:

<xsl:apply-templates select="." mode="specialHandling" />
相关问题