tags:

views:

206

answers:

3

I want to do the following:

  1. Declare a variable
  2. Go into a if-statement
  3. Overwrite the variable

XSL says I can't declare a variable twice, so what can I do to improve this step?

Another approach was to check if a variable is set at all. I did this, because i skipped the first step and declared the variable in the if-statement. In another if-statement I wanted to check if the variable exists at all.

+2  A: 

As you have discovered, the xslt standard requires the following:

A binding shadows another binding if the binding occurs at a point where the other binding is visible, and the bindings have the same name. It is an error if a binding established by an xsl:variable or xsl:param element within a template shadows another binding established by an xsl:variable or xsl:param element also within the template. It is not an error if a binding established by an xsl:variable or xsl:param element in a template shadows another binding established by an xsl:variable or xsl:param top-level element.

The solution here depends on what you really want to do.

  • If you want the variable to take on a single value for the entire template, but you want that value to depend on a condition, put an <xsl:if> or, more likely, <xsl:choose> inside the <xsl:variable> element
  • If you want the variable to take on a different value only within the <xsl:if> block, then either
    • Use a different variable name or
    • Put a <xsl:call-template> inside the <xsl:if> and define the variable again inside the called template.
Dan Menes
@Dimitre: Take it easy. Most people weren't born truly understanding the differences between functional and procedural languages. And it is most difficult for someone to "start thinking like a functional programmer" _before_ he "even tr[ies]" a functional language. Yes, this is a common mistake. That's why I'd rather show him how to change his thinking rather than berating him for not thinking right in the first place.
Dan Menes
@Dan-Menes Sorry, I moved my comment to the question -- where it belongs. :(As for your reply, I admire your patience and still doubt that this would be useful to the OP -- obviously, he needs months of practice.
Dimitre Novatchev
+2  A: 

Hi!

If you had mentioned the sample code where you felt you need to "Over write the variable" then any of the experts like Tomalak, Dimitre would have suggested you a better (alternative) option/suggestion.

Well. If its just your doubt (like how one can deal with a language where variables cannot be varied !!!??) then I bet you will get clarified as you go on practicing..

When I started my carrier with XSLT as a beginner even I had the same doubt .. but soon I realized .. We don't need variables to vary. ;) ;)

I feel its my pleasure to work with XSLT.
You can treat templates the way you deal with functions and procedures. (not precisely)
You can call them recursively ..
The extent of data-manipulation you can imagine, can be achieved in one or the other way.. (may not be much easier but not impossible atleast)..


coming back to your question, if you really need to change the value of the variable .. then feel free to define a new variable .. !? Use math operators, inbuilt function etc on the value of previous variable, and instead of assigning back to the same variable .. assign it to a new one .. and use it as you wish.


That may not be the efficient technique but can be a Step 1.

On the other hand .. you can send the expression .. [like translate(., abc, ABC)] as Parameters (Param) to other templates .. or can be written directly as output.. !! so as to avoid variable to vary. :))

infant programmer
Good answer to a vague question. +1
Dimitre Novatchev
@Dimitre, thanks :) I am glad to receive your compliments.
infant programmer