Using XSLT Variables and Parameters

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Using XSLT Variables and Parameters

XSLT Variables

In XSLT, you can use <xsl:variable> to define an XSLT variable. A variable in XSLT functions like a named constant in traditional programming languages, such as C/C++. You can use a variable to store values that might be needed repeatedly. A variable is also useful for caching context-sensitive values or a temporary tree during the transformation.

Once defined, an XSLT variable cannot be changed until it falls out of its scope. To reference a defined variable, prefix the $ sign to the value of the name attribute of the <xsl:variable> element.

An XSLT variable is global if it is declared as an immediate child element of the <xsl:stylesheet> element. A global variable can be used anywhere in the style sheet. A variable is local if it is declared within a template rule. The scope of a local variable is only within the context in which it is defined.

XSLT Parameters

You can use <xsl:param> to define an XSLT parameter. A parameter in XSLT functions like a variable in traditional programming languages, such as C/C++.

To reference a defined parameter, prefix the $ sign to the value of the name attribute of the <xsl:param> element.

An XSLT parameter is global if it is declared as an immediate child element of the <xsl:stylesheet> element. A global parameter can be used anywhere in the style sheet. A parameter is local if it is declared within a template rule. The scope of a local parameter is only within the context in which it is defined.

An XSLT parameter is a parameterized XSLT variable. Once defined, it cannot be changed until it falls out of its scope. However, a parameter can take a value passed in from outside its scope. For example, a global parameter can take a value passed in from a script in an HTML or ASP page. A local parameter can take a value passed in from the calling template rule.

The topics in this section refer to the weatherlog example, which will be added to throughout the section to illustrate the use of variables and parameters. The initial sample files appear at the beginning of the section. The finished sample files are provided at the end.

This section contains the following topics.