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.

Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114
ChrisBenyamin
  • 1,718
  • 4
  • 22
  • 39
  • 5
    That's not a description of WHAT you want to do -- it's a description of HOW you want to do it. ( And that how doesn't happen to work. ) Describe what you want to do. – Steven D. Majewski May 17 '10 at 22:12
  • Its not always a question of "what". My question contains a common procedure in many programming languages (set a variable and reset a variable). So I am just asking how you do that. Hope you can accept this kind of thinking of somebody, who is new in the XSL-usage. – ChrisBenyamin May 18 '10 at 09:50
  • 1
    It is always a question of "what you want to do". Even in regular programming languages, setting/resetting a variable only is a means to an end. And since re-assigning a variable in XSLT is impossible, it is crucial to know what you *actually* are trying to do. Otherwise any tips you may get will always be vague. – Tomalak May 18 '10 at 14:21
  • This is XSLT! XSLT is a functional language. In functional languages one cannot update the value of variables once they are initialized. This is by definition. Start thinking as a functional programmer, or don't even try XSLT if you can't. Whoever really cannot do without the capability to update variables, doesn't have any understanding of the fundamentals of the language. The best advice for such people is to learn more about functional programming, or keep away from functional languages – Dimitre Novatchev May 19 '10 at 05:02

3 Answers3

5

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 question (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 question .. 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. :))

Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114
2

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
  • 6,667
  • 1
  • 33
  • 35
  • 1
    @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 May 19 '10 at 03:58
  • 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 May 19 '10 at 05:04
1

Here are a few questions that have the same misconception about XSLT variables as you do.

Reading the answers to those questions is recommended. ;)

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628