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
Hi Sudheshna,
   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). [1]

I believe XSLT had these important design goals (like [1]), 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
paradigm.

As David wrote, XSLT variables have a lexical scope. For e.g please
consider this example:

<xsl:variable name="x" value="'hello'" />   [2]
<xsl:if test="true()">
    <xsl:variable name="x" value="'there'" />  [3]
</xsl:if>

Here variable [2] is not same as [3]. 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 [3] is within the enclosing "if" only. The scope of
variable [2] 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 [2] 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.
>
> Conditions:
> 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.



-- 
Regards,
Mukul Gandhi

Current Thread