How to improve variable overriding/overwriting in XSL?
I want to do the following:
- Declare a variable
- Go into a if-statement
- Overwrite the variable
XSL says I can't declare a variable twice, so what can I do 开发者_Go百科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.
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. :))
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.
Here are a few questions that have the same misconception about XSLT variables as you do.
- In XSLT how do you test to see if a variable exists?
- XSl:Variable - Condition to check whether value exist
- Actually XSLT Lookup (Store variables during loop and use in it another template)
Reading the answers to those questions is recommended. ;)
精彩评论