XSLT - Static Variables and Static Parameters in XSLT 3.0
1. Introduction
XSLT 3.0 provides several advanced features for transforming XML documents into different formats, such as HTML, XML, and plain text. One important feature is the use of static variables and static parameters. These features allow stylesheet developers to define values that are evaluated during the compilation or preparation of an XSLT stylesheet rather than during the actual transformation process.
Static variables and static parameters are useful when a stylesheet needs configuration values that remain fixed throughout a transformation. They help developers control stylesheet behaviour, select processing options, and configure reusable transformation logic.
Unlike ordinary variables and parameters, which are generally evaluated during transformation, static variables and parameters are evaluated during the static analysis phase. This distinction makes them useful for controlling how an XSLT processor prepares and executes a stylesheet.
2. What Are Static Variables in XSLT 3.0?
A static variable is a variable whose value is determined during the static evaluation phase of stylesheet processing. It is declared using the static="yes" attribute inside the xsl:variable element.
Static variables are commonly used to store configuration information, fixed values, and expressions that influence how a stylesheet is compiled.
For example, a developer may define a static variable to specify whether a particular feature should be enabled or disabled. The stylesheet can then use this value to determine which processing instructions or expressions should be included.
Example:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="debug-mode"
static="yes"
select="true()"/>
</xsl:stylesheet>
In this example, the variable named debug-mode is declared as a static variable. Its value is set to true(), meaning that the debugging configuration is enabled during stylesheet preparation.
3. What Are Static Parameters in XSLT 3.0?
Static parameters are values supplied to an XSLT stylesheet before the transformation begins. They are declared using the xsl:param element with the static="yes" attribute.
Unlike ordinary parameters, which can receive values during transformation, static parameters are used to configure the stylesheet during its compilation or preparation.
They are especially useful when the same stylesheet needs to support different processing configurations without requiring developers to create multiple stylesheet files.
For example, a stylesheet may need to generate different output structures depending on whether a particular feature is enabled.
Example:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="output-mode"
static="yes"
select="'simple'"/>
</xsl:stylesheet>
Here, the static parameter output-mode has a default value of simple. The processor can use this value during stylesheet preparation to determine the transformation configuration.
4. Difference Between Static Variables and Ordinary Variables
Static variables and ordinary variables differ mainly in when their values are evaluated and how they are used.
Static variables are evaluated during the static phase, while ordinary variables are generally evaluated during stylesheet execution.
The following table explains their differences.
|
Feature |
Static Variables |
Ordinary Variables |
|---|---|---|
|
Declaration |
|
|
|
Evaluation |
Static evaluation phase |
Runtime evaluation |
|
Purpose |
Configure stylesheet behaviour |
Store transformation values |
|
Availability |
Within their permitted static context |
Within their permitted scope |
|
Modification |
Cannot be reassigned |
Cannot be reassigned |
|
Common use |
Compilation-time configuration |
Processing XML data |
For example, a static variable may determine whether a particular transformation feature is included, while an ordinary variable may store the name of an XML element being processed.
5. Difference Between Static Parameters and Ordinary Parameters
Both static and ordinary parameters allow developers to pass values into an XSLT stylesheet. However, their evaluation stages and purposes are different.
Static parameters configure the stylesheet before transformation, whereas ordinary parameters provide values used during the actual transformation.
For example, a static parameter can determine which stylesheet configuration should be selected. An ordinary parameter can provide the title of a report that will appear in the generated output.
Consider the following example:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="language"
static="yes"
select="'English'"/>
<xsl:param name="report-title"
select="'Annual Report'"/>
<xsl:template match="/">
<html>
<body>
<h1>
<xsl:value-of select="$report-title"/>
</h1>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
In this example, language is a static parameter that can be used for stylesheet configuration. The report-title parameter is an ordinary parameter that supplies a value during transformation.
6. How Static Variables and Parameters Work
Static variables and parameters operate during the stylesheet preparation process. Their values are determined before the processor performs the main transformation.
The general process involves the following stages:
-
Declaration: The developer declares static variables or parameters using the appropriate XSLT elements and the
static="yes"attribute. -
Value Assignment: Default values are provided, or static parameter values are supplied by the XSLT processor or execution environment.
-
Static Evaluation: The processor evaluates the static expressions and determines the configuration of the stylesheet.
-
Stylesheet Preparation: The processor performs static analysis and prepares the stylesheet for execution.
-
Transformation: The prepared stylesheet processes the input XML document and generates the required output.
This process helps separate stylesheet configuration from the actual processing of XML data.
7. Practical Example of Static Parameters
Consider a company that maintains employee information in an XML document. The company wants to generate employee reports using different output configurations.
Instead of creating separate stylesheets for every configuration, developers can use a static parameter to control which processing option is selected.
Example:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="show-department"
static="yes"
select="true()"/>
<xsl:template match="/">
<html>
<body>
<h2>Employee Information</h2>
<xsl:for-each select="employees/employee">
<p>
Name:
<xsl:value-of select="name"/>
</p>
<xsl:if test="$show-department">
<p>
Department:
<xsl:value-of select="department"/>
</p>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
In this example, the static parameter show-department controls whether department information should be displayed.
When its value is true(), the department details are included in the output. When its value is false(), the department information is excluded.
The example demonstrates how static configuration can influence the behaviour of a stylesheet. In a production stylesheet, static parameters can also be used in static expressions and conditional compilation constructs to control which parts of the stylesheet are prepared.
8. Advantages of Static Variables and Static Parameters
Static variables and parameters provide several advantages when developing complex XSLT applications.
Improved Configuration: Developers can configure stylesheet behaviour without modifying the main transformation logic.
Reusability: A single stylesheet can support different configurations through static parameters.
Better Organisation: Configuration-related values can be separated from ordinary transformation variables and parameters.
Compile-Time Decisions: Certain processing decisions can be made during stylesheet preparation rather than repeatedly during transformation.
Maintainability: Centralised configuration makes it easier to understand, update, and manage complex stylesheets.
Potential Performance Benefits: Static evaluation can allow processors to resolve configuration-dependent expressions before runtime, although actual performance improvements depend on the processor and stylesheet.
9. Limitations and Important Considerations
Although static variables and parameters are useful, developers should understand their limitations.
Static variables cannot be used to store values that depend on XML nodes being processed dynamically during transformation. Their expressions must be valid in the static context.
Static parameters must be supplied at the appropriate stage by the XSLT processing environment. Changing an ordinary runtime parameter does not automatically change a static parameter.
Another important consideration is processor compatibility. XSLT 3.0 features require an XSLT 3.0-compatible processor. Older processors may not recognise static variables or static parameters.
Developers should also avoid using static configuration unnecessarily. If a value changes for each XML element or depends on input data, an ordinary variable or parameter is generally more appropriate.
10. Conclusion
Static variables and static parameters are important configuration features introduced in XSLT 3.0. They allow developers to determine stylesheet settings during the static evaluation and preparation stages, before the actual transformation takes place.
Static variables are useful for defining fixed configuration values, while static parameters allow external configuration values to be supplied to a stylesheet. Both features help developers create flexible, reusable, and maintainable transformation solutions.
By understanding the difference between static and ordinary variables and parameters, developers can make better decisions when designing XSLT applications. These features are particularly valuable in large XML processing projects where the same stylesheet must support different configurations and processing requirements.