Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between xsl:param and xsl:variable

Tags:

xslt

i am considering either one to use when defining an xsl:template name = myTemplate that will be called in another xsl file.

legacy code seems to use 'xsl:variable' when being used in xsl:template match="legacyTemplate"

so what's the difference between xsl:param and xsl:variable when using it in xsl:template name=myTemplate?

like image 949
bouncingHippo Avatar asked Aug 07 '12 20:08

bouncingHippo


People also ask

What is xsl param?

Definition and Usage The <xsl:param> element is used to declare a local or global parameter. Note: The parameter is global if it's declared as a top-level element, and local if it's declared within a template.

What is xsl variable?

Definition of XSLT Variable. XSLT variable is defined as special tags used to declare a local or global variable that we make use of to store any values. The declared variables are referenced within an Xpath expression. Once it is set we cannot overwrite or update the variables.

How do I change the value of a variable in XSLT?

Variables in XSLT are not really variables, as their values cannot be changed. They resemble constants from conventional programming languages. The only way in which a variable can be changed is by declaring it inside a for-each loop, in which case its value will be updated for every iteration.


1 Answers

The difference is that the value of an xsl:param could be set outside the context in which it is declared. For example, see:

<xsl:template ...>    <xsl:param name="p" select="'x'" />    <xsl:variable name="v" select="'y'" /> ... 

then you know that $v will always give you the string 'y'. But for $p the string 'x' is only a default: you will see a different value if the template is invoked with either xsl:apply-templates or xsl:call-template which contains an instruction such as:
<xsl:with-param name="p" select="'not x'" />

<xsl:param> may also be used outside xsl:template, at the top level in the stylesheet. The value of such a parameter may be set when the XSLT processor is called. How this is done depends on the processor and whether you call it from the command line or by program.

like image 173
Jainendra Avatar answered Oct 06 '22 13:10

Jainendra