none
delclaring global variable and reassign it in XSLT RRS feed

  • Question

  • Hi,

    I am having for each loop and having two condition as follows:

    <xsl:for-each select="//data/account/driver">

    tempVar = '0'

    <xsl:if test="concat(FirstName/text(),LastName/text()  = FullName/text()">

    tempVar = '1'

    do some stuff

    </xsl:if>

    <xsl:if test="concat(FirstName/text(),LastName/text()  != FullName/text()">

    <xsl:if test="$tempVar != '1'>

    do some stuff

    </xsl:if>

    </xsl:if>

    How can I do this ? in XSLT ?


    Thanks and Regards, Nitin.

    Friday, October 14, 2016 10:06 AM

Answers

  • Well, it looks like you just need to declare (and set) tempVar withig the xs:if that tests FullName and LastName.  Meaning, don't try to declare it first, then set it.  You have to declare and set Xslt variables at the same time.

    The declarative nature of Xslt means variables cannot be reassigned.

    • You can declare and set top level (global) variables and use them everywhere, but their value cannot change.
    • You can re-declare and set a variable in every execution of a scope.  In the sample you posted, the for-each is a scope so you can re-declare any variable in each loop.
    • When setting a top level variable, you still have access to the entire node tree so you can set it based on any content at any level.

    Friday, October 14, 2016 11:03 AM
    Moderator

All replies

  • HI Nitin,

    use

    <xsl:variable Name="xyz" Select="some xpath"><xsl:variable>
    

    Define it at the top of the xsl and it becomes global thenyou can use it through out the xsl.


    Mandar Dharmadhikari

    Friday, October 14, 2016 10:17 AM
    Moderator
  • HI Nitin,

    use

    <xsl:variable Name="xyz" Select="some xpath"><xsl:variable>

    Define it at the top of the xsl and it becomes global thenyou can use it through out the xsl.


    Mandar Dharmadhikari

    Hi mandar,

    Thanks for help ...but my question is diff.

    I want to reassign value to this variable if it goes to first condition and when calling second if condition if tempVar !=1 then go inside this if condition.


    Thanks and Regards, Nitin.

    Friday, October 14, 2016 10:27 AM
  • Hi Nitin,

    Such variable use is not supported in XSL.

    What you can do is create a global variable inside C# custom script in xslt and assign read from there.

    Create a variable and two methods, one that updates ur variable and other that reads it.

    public string myvar;

    public string GetVarValue()

    {

    return myvar;

    }

    public string UpdateVarValue(string var)

    {

     myvar = var;

    }

    Have this under userScripts in xsl.


    Pi_xel_xar

    Blog: My Blog

    BizTalkApplicationDeploymentTool: BizTalk Application Deployment Tool

    LinkedIn: LinkedIn

    Friday, October 14, 2016 10:37 AM
    Answerer
  • Hi Nitin,

    You can not do that..the variable value will be lost after the scope of each condition,global variable wil have the value through out the execution of your xslt but that value can not be changed...can you share more details like what logic you are trying to implement??that way we can help you in a more better way..

    Regards,


    Mandar Dharmadhikari

    Friday, October 14, 2016 10:47 AM
    Moderator
  • Hi Nitin,

    You can not do that..the variable value will be lost after the scope of each condition,global variable wil have the value through out the execution of your xslt but that value can not be changed...can you share more details like what logic you are trying to implement??that way we can help you in a more better way..

    Regards,


    Mandar Dharmadhikari

    As I explained in my question...

    what I am trying to do is that if FirstName,LastName is match with FullName and suppose I have 10 elements in my destination schema then if condition satisfied then map it with some other data else map same elements with diff data...

    for now let's say that I am hardcoding all the desitination schema elements so the hardcoding values will be diff in IF condition and diff for else condition.

    as I am using loop here ...so I am having multiple drivers and needs to check this condition for each of them


    Thanks and Regards, Nitin.

    Friday, October 14, 2016 11:00 AM
  • Well, it looks like you just need to declare (and set) tempVar withig the xs:if that tests FullName and LastName.  Meaning, don't try to declare it first, then set it.  You have to declare and set Xslt variables at the same time.

    The declarative nature of Xslt means variables cannot be reassigned.

    • You can declare and set top level (global) variables and use them everywhere, but their value cannot change.
    • You can re-declare and set a variable in every execution of a scope.  In the sample you posted, the for-each is a scope so you can re-declare any variable in each loop.
    • When setting a top level variable, you still have access to the entire node tree so you can set it based on any content at any level.

    Friday, October 14, 2016 11:03 AM
    Moderator