2014-02-07 50 views
0

在一個相當複雜的xslt文件中,一些元素將被處理兩次。這由具有參數的模板完成。xslt通過所有應用模板調用傳遞參數

<xsl:template macht="x"> 
     <xsl:param name="modus"/> 

    <!-- comon things to do for both cases --> 

     <xsl:choose> 
     <xsl:when test="$modus='case1'"> <!-- things to do in case 1 --> </xsl:when> 
     <xsl:when test="$modus='case2'"> <!-- things to do in case 2 --> </xsl:when> 
     </xsl:choose> 

    </xsl:template> 

的問題是:我不能簡單地套用或直接調用這個模板。元素x(這個模板有這兩種情況)適用於xml輸入。幾乎所有的祖先元素在它實際到達x之前都必須進行處理(在這兩種情況下)。

這兩個案件的調用是在最高級別。 在HTML它會是這樣

<body> 
<h1>Case 1</h1> 
<xsl:apply-templates><xsl:with-parameter name="modus" select="case1"/> 

<h1>Case 2</h1> 
<xsl:apply-templates><xsl:with-parameter name="modus" select="case2"/> 
</body> 

所以。我怎樣才能確保參數到達x的模板?

當然,我可以

<xsl:param name="modus"> 

<!-- What ever content here --> 

<xsl:apply-templates><xsl:with-parameter name="modus" select="$modus"/></apply-templates> 

取代對x的每一個祖先元素模板內的所有

<xsl:apply-templates/> 

調用但是,這將意味着很多的努力。有一個更好的方法嗎?

回答

1

XSLT 2.0具有隧道參數,例如,與

<xsl:apply-templates> 
    <xsl:with-param name="modus" tunnel="yes" select="'foo'"/> 
</xsl:apply-templates> 

<xsl:template match="bar"> 
    <xsl:param name="modus" tunnel="yes"/> 
    ... 
</xsl:template> 

你不必在模板中明確傳遞的參數爲bar祖先。因此,使用像Saxon 9這樣的XSLT 2.0處理器就可以做到這一點。