XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Variables and parameters
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)
XSLT’s functional, side-effect-free nature has consequences that may surprise procedural programmers. A prime example is its concept of a “variable”: a variable whose value cannot change? Is that even a variable? It is time to examine this.
XSLT is declarative and rule-based
A brief theoretical recap: what do functional, declarative and rule-based mean? A procedural language models processing tasks and their order; XSLT instead describes declaratively the desired transformation result: What is output in which case?
This uses so-called template rules, resembling subroutines, activated through pattern comparisons (pattern matching) against structures in the source document.
Template rule
In a simple case, a template rule handles an element of a particular name, becoming active when processing encounters it.
Thus, rule order in the stylesheet is irrelevant and independent of document structure. Activation is determined only by source element order, and need not and should not be specified by the author.
XSLT has no side effects
Less obviously, XSLT was designed without side effects. The price is that global variable values cannot change while a stylesheet runs: there is no assignment statement. In this sense, XSLT is also functional.
Rule call order can be irrelevant only if calls do not change runtime parameters. If rule A sets a global variable X used by rule B, output depends on whether A or B runs first.
Side effects are undesirable
An XSLT template rule should therefore only produce results, without modifying conditions relevant to other rules. Rules are functional in that their activation always produces the same result.
Freedom from side effects makes transformation results unambiguous, independent of a required rule activation order. It also enables the processor to optimize processing.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn