How do I reassign a variable in XSLT? [duplicate]
Asked Answered
M

2

0

I want to be able to do some basic reassignments to variables in XSLT. How can this be achieved?

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();
    }
}

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 &gt; 12">
    <xsl:param name="currentMonth" select="($currentMonth + $count) - 12"/>
</xsl:if>

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....]
    }
}
Moneymaking answered 5/9, 2013 at 13:7 Comment(0)
R
2

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
}
Requite answered 5/9, 2013 at 13:47 Comment(2)
When I append the zero to m2, would I have to assign a different value again?Moneymaking
If you append a zero to a value then you are creating a new value, yes.Requite
R
-2

I just almost believe the viewpoint in other replies, before I test the follow code that can really run well in xlst proccessor saxon-he 9.8.0.12 my code :

    <?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

for your question. the solution maybe like that:

<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>
Rouault answered 2/12, 2019 at 2:17 Comment(4)
note that the xslt processor must surpport xslt 2.0 aboveRouault
I am afraid you are completely missing the point of the question and confusing the shadowing of a variable (declaring another variable with the same name) with assigning another value to the same variable. If reassigning were possible, the result here would be different: xsltfiddle.liberty-development.net/bFWR5EkCathedral
yes. it's anthor variable override the premier. but the effect is some with reassign completely. @CathedralRouault
ok. I append some words in answer to the just quesions clearly. @CathedralRouault

© 2022 - 2024 — McMap. All rights reserved.