2011-05-13 79 views
0

我想從template1中調用template2,併爲其提供template1中的參數。XSLT:使用提供參數的模板中的參數調用模板

現在,我有這樣的事情:

<xsl:template name="template1" match="home/sentences/sentence"> 
     <xsl:if test="something..."> 
      <new_sentence> 
       <!-- ...other unrelated stuff... --> 

       <xsl:apply-templates select="key('get_sentence_attribute', tokenRef/@tokID)"/> 

       <!--Here I point to the template I made for tokens. 
       But I also wish to provide it with the value returned by get_sentence_attribute --> 
       <xsl:apply-templates select="../../tokens"/> 
      </new_sentence> 
     </xsl:if> 
</xsl:template> 

<xsl:template name="template2" match="home/tokens"> 
     <!-- ... --> 
</xsl:template> 

基本上我需要確保我的標記模板選擇的值,匹配sentence_attribute我在文章模板得到。我搜索了一下,發現了<xsl:with-param>元素;但是對我來說這很讓人困惑,我甚至不確定這是否是我需要的。

感謝您的幫助!

回答

2
<!-- 1. store your results in a variable --> 
<xsl:variable name="result"> 
<xsl:apply-templates select="key('get_sentence_attribute', tokenRef/@tokID)"/> 
</xsl:variable> 

<!-- 2. call your template with a param value --> 
<xsl:call-template name="named-template"> 
    <xsl:with-param name="param1" value="$result"/> 
</xsl:call-template/> 

... 
... 

<!-- 3. you need to declare your template to accept a parameter --> 
<xsl:template name="named-template"> 
    <xsl:param name="param1"/> 

    <!-- do stuff with $param1--> 
</xsl:template> 
+0

我想要一個「本地」變量,在'template1'內創建,並傳遞給template2。那麼我應該在'template2'的'template1'和'3'裏面加入'1.'和'2'嗎?我得到了「解析xslt樣式表失敗」 – Spectraljump 2011-05-13 15:07:05

+0

我遇到了一些問題,但試驗和錯誤修復了所有問題......您確實有一個語法錯誤:''。我已經接受了你的答案,但修正了'/>'。此外,在將來,當您發佈答案時,請嘗試根據問題的示例代碼回答一個示例;它使事情變得更容易。 – Spectraljump 2011-05-13 16:11:32