2013-02-14 61 views
2

我要动态地改变基于源XML的属性的应用模板模式,就像这样:XSLT模板模式 - XPath计算

<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> 

是否有可能以评估模式属性中的XPath?还有其他方法吗?

谢谢!

回答

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" />