2011-05-13 78 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