有沒有一種方法可以檢查我的變量在選擇屬性和 請撥打兩個用戶功能TestFx
或TestFx2
中的一個。現在我知道我可以使用 xsl:if
或xsl:choose
,但只是想知道是否有另一種方式。
這裏是一個完整的演示如何做到這一點。這說明了如何在XSLT 1.0和2.0中實現高階函數的基本原理 - 在FXSL library for functional programming with XSLT中使用。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="my:user" xmlns:my="my:my">
<xsl:output method="text"/>
<my:functions>
<my:F1/>
<my:F2/>
</my:functions>
<xsl:variable name="vMyFuncs" select="document('')/*/my:functions/*"/>
<xsl:param name="phasTextArea" select="true()"/>
<xsl:param name="pText" select="'Some Text'"/>
<xsl:template match="/*">
<xsl:variable name="vFunc" select=
"$vMyFuncs[1][$phasTextArea] | $vMyFuncs[2][not($phasTextArea)]"/>
<xsl:apply-templates select="$vFunc"/>
</xsl:template>
<xsl:template match="my:F1">
<xsl:value-of select="user:TestFx($pText)"/>
</xsl:template>
<xsl:template match="my:F2">
<xsl:value-of select="user:TestFx2($pText)"/>
</xsl:template>
<msxsl:script language = "c#" implements-prefix = "user">
public string TestFx(string text)
{
return "Text is: " + "'" +text + "'";
}
public string TestFx2(string text)
{
return string.Format("Text length is: {0}", text.Length);
}
</msxsl:script>
</xsl:stylesheet>
當這種轉化是在任何XML文檔(未使用),在對需要的正確的結果(user:TestFx()
稱爲)施加產生:
Text is: 'Some Text'
如果我們通過設置修改上面的代碼$phasTextArea
至false()
,結果現在顯示此時user:TestFx2()
已被稱爲:
Text length is: 9
而且,如所承諾的,轉換沒有明確的XSLT條件指令(xsl:choose
或xsl:if
)。
此外,我們不計算字符做substring()
不必要的竅門。
Rod,是的,有這樣的一種方式,畢竟它並不那麼困難或棘手。 – 2013-04-12 02:57:41