Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare global variable in XSLT ,re-assign a value locally

Tags:

variables

xslt

I can declare a variable “myVariable” with a value “111” in the global scope. But how can I re-assign a value locally. Or is there a alternative way to achieve this.

Please help. Thank you. Ravi

like image 746
Jagath Jayasinghe Avatar asked Jun 12 '13 13:06

Jagath Jayasinghe


People also ask

How do I assign a value to a global variable in XSLT?

XSLT is not an imperative programming language where you can change the value of a variable. If you want to pass a value from one template to another then you can use a parameter e.g. The fact that you need to modify an <xsl:variable> shows that you are not thinking in XSLT.

How do you declare a variable in XSLT?

XSLT <xsl:variable>The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!

How do you add two variables in XSLT?

You use the <xsl:variable> element to declare a variable and assign a value to it. Remember that these two must always be done at the same time in XSLT, you can't declare a variable and later assign it a value.


1 Answers

You can re-define the same variable inside a template:

<xsl:variable name="myVariable" select="'111'"/>

<xsl:template match="/">
  <xsl:variable name="myVariable" select="'112'"/>
  . . . 
</xsl:template>

Note though that 'variables' in XSLT are actually constant - you are not re-assigning a different value to the same variable, you are re-defining it inside the template - outside the template myVariable will still have the value 111.

like image 88
MiMo Avatar answered Nov 04 '22 15:11

MiMo