2013-09-05 23 views
0

我希望能夠對XSLT中的變量做一些基本的重新分配。這怎麼能實現?如何在XSLT中重新分配變量?

我只是希望能夠將其轉換爲XSLT(忽略appendMonthWithZero()函數):

if(currentMonth + count > 12) //If we get past December onto a new year we need to reset the current month back to 01 
{ 
    currentMonth = (currentMonth + count) - 12; //e.g. 9 + 4 = 13, 13 - 12 = 1 (January). Or 9 + 11 = 20, 20 - 12 = 8 (August) 
    if(currentMonth < 10) 
    { 
     currentMonth = appendMonthWithZero(); 
    } 
} 

到目前爲止,我有這個在XSLT,但它不工作。我通過這12次循環,所以我要不斷修改currentMonth除其他變量:

<xsl:if test="$currentMonth + $count &gt; 12"> 
    <xsl:param name="currentMonth" select="($currentMonth + $count) - 12"/> 
</xsl:if> 

實際上,這就是我想要的僞整體做(http://pastebin.com/WsaZaKnC):

currentMonth = getCurrentMonth(); 
actualDateWithZero = appendMonthWithZero(); 
docs = getFlightResults(); 
monthsArray = ['Jan', 'Feb', 'Mar'.......]; 

for(count = 0; count < 12; count++) 
{ 
    outboundMonth = subString(doc[count+1].getOutboundMonth()); 

    if(currentMonth + count > 12) //If we get past December onto a new year we need to reset the current month back to 01 
    { 
     currentMonth = (currentMonth + count) - 12; //e.g. 9 + 4 = 13, 13 - 12 = 1 (January). Or 9 + 11 = 20, 20 - 12 = 8 (August) 
     if(currentMonth < 10) 
     { 
      currentMonth = appendMonthWithZero(); 
     } 
    } 

    //A price is available. 
    //Second check is for when we get past a new year 
    if(currentMonth + count == outboundBoundMonth || currentMonth == outboundMonth) 
    { 
     //Get rest of data from doc etc etc 
     //Set up divs etc etc 
     //Get string month with displayed Month [Jan, Feb, Mar....] 
    } 

    //Else no price available for this month 
    else 
    {  
     //display price not available 
     //Get string month with displayed Month [Jan, Feb, Mar....] 
    } 
} 

回答

2

XSLT是一種聲明性語言,它不使用有狀態變量。您需要弄清楚如何將輸出表達爲輸入的函數,而不是想辦法向計算機提供低級程序指令。

在你的情況下,它似乎非常簡單;只是使用不同的變量:

if(currentMonth + count > 12) { 
    m2 = (currentMonth + count) - 12; 
    if (m2 < 10) then appendMonthWithZero(m2) else m2; 
} else { 
    currentMonth 
} 
+0

當我將m2添加到m2時,是否需要再次分配不同的值? –

+0

如果你爲一個值附加一個零,那麼你正在創建一個新值,是的。 –