[XSL-LIST Mailing List Archive Home]
Re: [xsl] Variable value change based on condition
Subject: Re: [xsl] Variable value change based on condition|
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Tue, 5 Oct 2010 17:03:44 +0530
Here's a little explanation please.
On Tue, Oct 5, 2010 at 3:09 PM, sudheshna iyer <sudheshnaiyer@xxxxxxxxx> wrote:
> I need a variable whose value can be changed based on the if conditions.
First of all, it's important to know that variables in XSLT are
immutable (i.e their values cannot change once a value is bound
initially to an XSLT variable). 
I believe XSLT had these important design goals (like ), with the
objective to prohibit side effects for computations in an XSLT
environment, and not having fixed ordering of computing XSLT
instructions. These are an essential characteristics of functional
programming paradigm. Mutable variables produce side effects, which is
not desirable in XSLT or for that matter in an functional programming
As David wrote, XSLT variables have a lexical scope. For e.g please
consider this example:
<xsl:variable name="x" value="'hello'" /> 
<xsl:variable name="x" value="'there'" /> 
Here variable  is not same as . These are different variables,
but their names are same ("x") -- this is a classic fallacy when we
look at this concept from procedural programming point-of view. The
scope of variable  is within the enclosing "if" only. The scope of
variable  is for all XSLT instructions, which are following sibling
to "if" instruction (i.e for all following siblings after /xsl:if>),
and for any-of XSLT instruction between  and the starting tag of
xsl:if (i.e <xsl:if ...)
> I have 3 input variables (inputVar1, inputVar2, inputVar3) and 3 output variables (outputTempVar1, outputVar1, outputVar2)
> I need to copy these input variables to output variables based on some condition.
> If all the 3 input variables are present, map inputVar1 -> outputTempVar1, map inputVar2 -> outputVar1 and map inputVar3 -> outputVar2.
you say that, variable is present if it's not empty (does this mean,
it's value is a zero length string, but that's defined in scope?)
> So can I change the value of the output variable? How can I achieve the above in xslt 1.0.
No problems, but had you been using XPath 2.0, writing such a logic
would have been quite simpler :)
I think, you may explain the problem a little more, for us to think
about the most appropriate solution.
And I hope, this helps.