如何在 XSLT 中重新分配变量? [英] How do I reassign a variable in XSLT?
问题描述
我希望能够对 XSLT 中的变量进行一些基本的重新分配.如何实现?
I want to be able to do some basic reassignments to variables in XSLT. How can this be achieved?
我只是希望能够将其转换为 XSLT(忽略 appendMonthWithZero() 函数):
I just want to be able to convert this to XSLT (ignoring the appendMonthWithZero() function):
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
:
So far I have this in XSLT but it doesn't work. I am looping through this 12 times so I want to keep modifying currentMonth
amongst other variables:
<xsl:if test="$currentMonth + $count > 12">
<xsl:param name="currentMonth" select="($currentMonth + $count) - 12"/>
</xsl:if>
这基本上是我试图用伪代码(http://pastebin.com/WsaZaKnC):
This is essentially what I'm trying to do overall in pseudocode (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....]
}
}
推荐答案
XSLT 是一种声明性语言,它不使用有状态变量.您需要弄清楚如何将您的输出表示为输入的函数,而不是想办法向计算机提供低级程序指令.
XSLT is a declarative language, it doesn't use stateful variables. You need to work out how to express your output as a function of the input, rather than thinking of a way to give low-level procedural instructions to the computer.
就您而言,这似乎很简单;只需使用不同的变量:
In your case it seems very straightforward; just use different variables:
if(currentMonth + count > 12) {
m2 = (currentMonth + count) - 12;
if (m2 < 10) then appendMonthWithZero(m2) else m2;
} else {
currentMonth
}
这篇关于如何在 XSLT 中重新分配变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!