2012-03-08 24 views
6

我正在使用下面的方法將值賦給變量。解除後分配給<xsl:variable>

<xsl:variable name="NewValue"> 
    <xsl:value-of select="normalize-space(//root/id/amount)"/> 
</xsl:variable> 

賦值後我想給新的值賦給同一個變量。像這樣: -

<xsl:variable name="NewValue" select="normalize-space(//root/id/amountnew)"> 

有沒有什麼辦法呢?


這裏XML的樣品,我有:

<VolLien> 
    <Vest_DocType>SDD</Vest_DocType> 
    <Vest_Instrument>395072</Vest_Instrument> 
    <Vest_OfOfficialEntity>eee</Vest_OfOfficialEntity> 
    <Vest_RecDate>12/24/2009</Vest_RecDate> 
    <Vest_Grantee1>abc dd</Vest_Grantee1> 
    <Vest_Grantor1>sss</Vest_Grantor1> 
    <Vest_RejectCode /> 
    <Vest_RejectReason /> 
    <Vest_ImageNum> </Vest_ImageNum> 
</VolLien> 

我的問題是我需要獲得最新的特別<Vest_DocType>(說SDD)<Vest_RecDate>然後我需要在整個XML任何日期搜索這在此(相同的SDD)的<Vest_RecDate>之前。

如果再單獨提出該特定部分(<VolLien>)並再次提出最新。如果我可以重新分配,我會定位節點並獲取與之相關的值。現在我正在使用另一個循環來做這件事。如果有東西在那裏,我可以避免extrs循環。

+0

請添加您正在使用的XML的示例,以及您要做什麼的簡短描述。 – Tomalak 2012-03-08 06:40:58

+0

嗨,我正在做一些規則操縱xsl與我的XML數據。對於某個場景,我想重新分配已經聲明的變量的另一個值。這是我尋找這樣的任何選項的原因,,無論如何謝謝你爲我快速回復和有價值的輸入 – Varun 2012-03-08 07:02:20

+0

@Varun我沒有完全理解你的要求,但看看我的修改答案,也許這會讓你開始。 *(PS:您可以移除評論,因爲我將它們移到問題中。)* – Tomalak 2012-03-08 15:57:20

回答

8

編號XSLT變量是隻讀的。他們不能分配多次。

XSLT不是像PHP這樣的命令式編程語言。重新分配變量既不可能也不必要。


編輯:根據您的評論:

我的問題是我需要獲得最新的特別<Vest_DocType>(說SDD),然後我需要在整個XML要搜索的任何日期之前<Vest_RecDate>這(相同的SDD)。

這裏是一個XSLT片段,可以爲你做到這一點:

<!-- a key to retrieve all <VolLien> nodes of a particular DocType --> 
<xsl:key name="VolLien-by-DocType" match="VolLien" use="Vest_DocType" /> 

<xsl:template match="/"> 
    <xsl:call-template name="latest-VolLien"> 
    <xsl:with-param name="DocType" select="'SDD'" /> 
    </xsl:call-template> 
</xsl:template> 

<!-- this template processes specified <VolLien> nodes in the right order --> 
<xsl:template name="latest-VolLien"> 
    <xsl:param name="DocType" select="''" /> 

    <xsl:for-each select="key('VolLien-by-DocType', $DocType)"> 
    <!-- sorting is a little complicated because you 
     use dd/mm/yyyy instead of yyyymmdd --> 
    <xsl:sort 
     select=" 
     number(substring(Vest_RecDate, 7, 4)) + 
     number(substring(Vest_RecDate, 4, 2)) + 
     number(substring(Vest_RecDate, 1, 2)) 
     " 
     data-type="number" 
    /> 
    <!-- do something with last <VolLien> (in date order) --> 
    <xsl:if test="position() = last()"> 
     <xsl:apply-templates select="." /> 
    </xsl:if> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="VolLien"> 
    <!-- this template will actually process the nodes, 
     do whatever you want here --> 
</xsl:template> 
2

加成託默勒格的答案。

一個變量對於聲明和賦值的塊(聲明和賦值同時發生)是有限/局部的。

假設例子:

<xsl:template match="Node1"> 
    <xsl:variable name="test" select="'test_value'"/> 
    <xsl:for-each select="foo"> 
     <xsl:variable name="var1" select="."/> 
     <xsl:value-of select="$var1"/> 
    </xsl:for-each> 
    <xsl:for-each select="bar"> 
     <xsl:value-of select="$var1"/> 
     <!--The variable "$var1" is out of scope.. So above statement is wrong--> 
    </xsl:for-each> 
    </xsl:template> 

    <xsl:template match="Node2"> 
    <xsl:value-of select="$test"/> 
    <!--The variable "$test" is out of scope.. So above statement is wrong too!--> 
    </xsl:template> 
4

而且除了以前的答案:你正在試圖解決使用您從其他編程語言學會了低級別的當務之急技術的一些問題。爲了幫助您使用適合於XSLT的聲明性方法來解決問題,我們需要對問題進行說明。儘可能使其水平高 - 將輸出描述爲輸入的函數,而不是描述您認爲需要從一個輸入到另一個輸入的計算步驟。