2011-11-23 62 views
0

我無法將參數傳遞給模板。如何將一個參數從一個XSLT模板傳遞到另一個?

<!-- // Product/Instances --> 
<xsl:template match="/data/products/instances"> 
    <ul> 
     <xsl:apply-templates select="item"> 
      <xsl:with-param name="idp" select="@id"/> 
     </xsl:apply-templates> 
    </ul> 
</xsl:template> 

<!-- // Product/Instances/Instance --> 
<xsl:template match="/data/products/instances/item"> 
    <xsl:param name="idp"/> 
    <p>$idp: <xsl:value-of select="$idp"/></p> <!-- $idp is empty --> 
    <xsl:for-each select="/data/instances/entry"> 
     <xsl:if test="@id = $idp"> 
      <p><xsl:value-of select="code"/></p> 
     </xsl:if> 
    </xsl:for-each> 
</xsl:template> 

/data/products/instances/item具有名爲id的屬性,其具有的整數的值。

雖然第二個模板及其for-each循環正在處理中(我通過輸出其中的虛擬輸出來測試它們),但$idp參數的值未傳遞給第二個模板。

謝謝。

回答

1

您需要顯示足夠的詳細信息才能重現問題,否則很難判斷出了什麼問題。

我認爲你不需要任何參數,你應該使用一個關鍵

<xsl:key name="k1" match="data/instances/entry" use="@id"/> 

<!-- // Product/Instances --> 
<xsl:template match="/data/products/instances"> 
    <ul> 
     <xsl:apply-templates select="item"/> 
    </ul> 
</xsl:template> 

<!-- // Product/Instances/Instance --> 
<xsl:template match="/data/products/instances/item"> 

    <xsl:for-each select="key('k1', @id)"> 

      <p><xsl:value-of select="code"/></p> 

    </xsl:for-each> 
</xsl:template> 
3

的問題是,當你的應用模板,你目前的情況下是在實例元素等@id的屬性指的是實例元素的屬性ID,而不是屬性項目要選擇的元素(尚未在該點選擇)。

在給出的示例中,實際上並不需要傳入參數。只需在匹配模板中使用一個變量即可。在XSL的Insteaf:PARAM,請執行下列操作:

<xsl:variable name="idp" select="@id"/> 

這將得到id的值屬性爲你,因爲你是定位在這一點上,項目元素。

+0

+1一個正確的答案。 –

+0

謝謝!您提供的'xsl:variable'解決方案是可行的。關於'xsl:with-param',我發現即使在執行''時,我也無法獲得第二個整數輸出模板。我也試過'item/@ id',認爲上下文可能是問題。 –

相關問題