如何在XSLT中重新分配变量?

时间:2013-09-05 13:07:11

标签: variables 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 个答案:

答案 0 :(得分:2)

XSLT是一种声明性语言,它不使用有状态变量。您需要弄清楚如何根据输入表达输出,而不是考虑向计算机提供低级程序指令的方法。

在你的情况下,它似乎非常简单;只需使用不同的变量:

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

答案 1 :(得分:-1)

我几乎相信其他答复中的观点,然后再测试可以在xlst处理程序saxon-he 9.8.0.12中很好运行的以下代码 我的代码:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    exclude-result-prefixes="xs map"
    version="2.0">

    <xsl:template match="/">
        <xsl:variable name="i1" select="123" as="xs:integer"/>
        <xsl:variable name="s1" select="'abcd'" as="xs:string"/>
        <xsl:variable name="d1" select="234.5" as="xs:float"/>

        <!-- we test that variable v1 can be assignment multi times and it is ok.  -->
        <xsl:variable name="v1" select="$i1"/>
        v1 is: <xsl:value-of select="$v1"/>
        <xsl:variable name="v1" select="$s1"/>
        v1 is: <xsl:value-of select="$v1"/>
        <xsl:variable name="v1" select="$d1"/>
        v1 is: <xsl:value-of select="$v1"/>

        <xsl:variable name="map1" select="map{'haha':119, 'good':110}"/>
        <xsl:variable name="map2" select="map:put($map1, 'go', 122)"/>
        <xsl:variable name="map1" select="map:put($map2, 'hello', 999)"/>
        map1(haha) is <xsl:sequence select="$map1?haha"/>
        map1(haha) is <xsl:sequence select="$map1?go"/>
        map1(hello) is <xsl:sequence select="$map1?hello"/>
    </xsl:template>

</xsl:stylesheet>

the screenshort of running result

针对您的问题。解决方案可能是这样的:

<xsl:template match="/" priority="20">
        <xsl:variable name="month" select="9"/>
        <xsl:variable name="count" select="5"/>
        <xsl:variable name="month" select="if (($month + $count) &gt; 12) then ($month + $count) mod 12 else $month"/>
        month is:<xsl:value-of select="$month"/>
</xsl:template>