Re: [xsl] Variable value change based on condition

Subject: Re: [xsl] Variable value change based on condition
From: sudheshna iyer <sudheshnaiyer@xxxxxxxxx>
Date: Tue, 5 Oct 2010 05:59:33 -0700 (PDT)
Thank you all, for the reply.

This is my requirement:
====
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:
1. If all the 3
input variables are present (element present with no blanks), map inputVar1 ->
outputTempVar1, map inputVar2 -> outputVar1 and map inputVar3 -> outputVar2.
2. If there are only two input variables present (inputVar1, inputVar2), map
inputVar1 -> outputVar1 and map inputVar2 -> outputVar2.
3. If there is only
one input variable is present (inputVar1), map inputVar1 -> outputVar1.   

So
output variables get their values based on input variable's presence (element
being present and not empty). I am trying to transform one xml to another. So
input variables come from input xml.
====

Can this be achieved in xslt 1.0?
--- On Tue, 10/5/10, Mukul Gandhi <gandhi.mukul@xxxxxxxxx> wrote:

> From:
Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
> Subject: Re: [xsl] Variable value
change based on condition
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Date:
Tuesday, October 5, 2010, 7:33 AM
> 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
> 
>
--~------------------------------------------------------------------
>
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> To
unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> or e-mail:
<mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx>
> --~--

Current Thread