2015-02-09 19 views
0

我創建了一個全局變量,並在兩個模板其被用於我能夠訪問我第一個模板,沒能獲得在第二模板中的值。下面是我的工作方式如何在多個模板標籤訪問全局變量的值

<xsl:variable name="currentValue"></xsl:variable> //global variable declaration 

    <xsl:template match="/"> 
    <xsl:variable name="unique-accounts" select="/*/*/*/accountId/text()generate-id()=generate-id(key('account-by-id', .)[1])]"/> 
<xsl:for-each select="$unique-accounts"> 
     <xsl:variable name="currentValue" select="current()"/> 
     <xsl:value-of select="$currentValue"/> //here value is printing 
     <xsl:apply-templates select="//secondTemplate"/> 
</xsl:for-each> 
</xsl:template> //close od first template 

<xsl:template match="secondTemplate"> 
    <xsl:value-of select="$currentValue"/> //here value is not printing 
</xsl:template> 

回答

1

如果我按照你的邏輯代碼正確(這不是完全肯定),你已經聲明瞭一個全局變量爲:

<xsl:variable name="currentValue"></xsl:variable> 

即作爲空。然後,您調用這個全局變量您模板中:

<xsl:template match="secondTemplate"> 
    <xsl:value-of select="$currentValue"/> 
</xsl:template> 

並得到一個空的結果 - 這正是你應該期望什麼。

在您第一模板聲明:

<xsl:variable name="currentValue" select="current()"/> 

覆蓋全局變量聲明爲模板的範圍(更準確地說,報關和他們的後代以下的兄弟姐妹 - 但因爲聲明是你在模板中做的第一件事情,所以它歸結爲同樣的事情)。

在更多的技術術語,綁定模板內建立陰影的具有約束力的頂級成立xsl:variable元素:
http://www.w3.org/TR/xslt/#dt-shadows

+0

有來自從第一template..so,它可以用來或第二模板消耗我的情況下,商店template..here內的任何工作了存儲值到全局變量 – Brittas 2015-02-09 10:59:12

+0

你可以把它作爲一個參數* *當你打電話/申請其他模板。 – 2015-02-09 11:10:20

+0

@Brittas是這個問題的回答?如果是,請通過接受答案關閉它。 – 2015-02-19 10:16:32

0

變量在XSLT命名值,它們不是存儲單元中你可以在不同的時間放置不同的值。這是聲明式和程序式編程之間的根本區別。

如果你想解釋一下你正在試圖解決的問題(也就是輸入和轉換的輸出),那麼我敢肯定,我們可以解釋如何把它寫在XSLT。從完全錯誤的方法到解決方案的逆向工程是不可能的。