看來,如果我不能在 call-template元素的name屬性使用XPath。我怎樣才能解決這個問題?
問得好,+1。
你不能。但是,你可以改用<xsl:apply-templates>
。
這裏是一個快速演示:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:double="double" xmlns:incr="incr" xmlns:my="my:my"
exclude-result-prefixes="double incr my"
>
<xsl:output method="text"/>
<double:double/>
<incr:incr/>
<xsl:variable name="vFuncDouble"
select="document('')/*/double:*[1]"/>
<xsl:variable name="vFuncIncr"
select="document('')/*/incr:*[1]"/>
<xsl:function name="my:double">
<xsl:param name="arg1" />
<xsl:sequence select="2*$arg1"/>
</xsl:function>
<xsl:function name="my:incr">
<xsl:param name="arg1" />
<xsl:sequence select="1+$arg1"/>
</xsl:function>
<xsl:template name="double" match="double:*">
<xsl:param name="arg1"/>
<xsl:sequence select="my:double($arg1)"/>
</xsl:template>
<xsl:template name="incr" match="incr:*">
<xsl:param name="arg1"/>
<xsl:sequence select="my:incr($arg1)"/>
</xsl:template>
<xsl:function name="my:apply">
<xsl:param name="pFun" as="element()"/>
<xsl:param name="arg1"/>
<xsl:apply-templates select="$pFun">
<xsl:with-param name="arg1" select="$arg1"/>
</xsl:apply-templates>
</xsl:function>
<xsl:template match="/">
<xsl:sequence select="my:apply($vFuncIncr, my:apply($vFuncDouble,2))"/>
</xsl:template>
</xsl:stylesheet>
當這種轉變是在任何XML文檔(未使用)應用,想要的結果產生:
5
待辦事項:
人們可以通過爲PARAMET ER(第一個參數),以my:apply()
任何「功能」和my:apply()
將它應用到它的第二個參數。
在XSLT 1.0和XSLT 2.0使用此相同的原則the FXSL library實現高階函數(HOFs) - read more here。
在即將XPath 3.0功能是在Xpath Data Model(XDM)的第一次第一類對象。
您是否嘗試過使用應用模板和模板規則來解決您的問題? call-template指令不接受名稱屬性中的xpath表達式,您必須明確設置名稱。 – riff
除了您似乎試圖重新創建xsl:apply-templates之外,您的XPath表達式ancestor :: local-name()不是有效的XPath表達式,並且如果是這樣,它可能會選擇多個名稱,因爲一個節點有幾個祖先。 –