2011-12-02 103 views
2

看來,如果我不能在調用模板元素的name屬性使用XPath。我怎樣才能解決這個問題?任何幫助/想法都會很棒!XSLT調用模板名稱屬性

<xsl:for-each select="child::knomaddb/Content/Videos"> 
     <xsl:result-document method="xhtml" href="{local-name()}.html"> 
      <html> 
       <body> 
        <h1>Knomad</h1> 
        <h2>{local-name()} Videos</h2> 
        <table border="1"> 
         <tr bgcolor="#9acd32"> 
          <th>Title</th> 
          <th>Video</th> 
          <th>Description</th> 
          <th>Comments</th> 
         </tr> 
         <xsl:for-each select="Video"> 
          <xsl:call-template name="{ancestor::local-name()}"/> 
         </xsl:for-each> 
        </table> 
       </body> 
      </html> 
     </xsl:result-document> 
    </xsl:for-each> 
+3

您是否嘗試過使用應用模板和模板規則來解決您的問題? call-template指令不接受名稱屬性中的xpath表達式,您必須明確設置名稱。 – riff

+0

除了您似乎試圖重新創建xsl:apply-templates之外,您的XPath表達式ancestor :: local-name()不是有效的XPath表達式,並且如果是這樣,它可能會選擇多個名稱,因爲一個節點有幾個祖先。 –

回答

1

看來,如果我不能在 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)的第一次第一類對象。