2013-02-14 81 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" />