Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Although template rules are a characteristic feature of the XSLT language, we'll see that this is not the only way of writing a stylesheet. In Chapter 17, I will describe four different design patterns for XSLT stylesheets, only one of which makes extensive use of template rules. In fact, the
Hello
World
stylesheet I presented earlier in this chapter doesn't make any real use of template rules; it fits into the design pattern I call
fill-in-the-blanks
, because the stylesheet essentially contains the fixed part of the output with embedded instructions saying where to get the data to put in the variable parts.
Types Based on XML Schema
I have described three characteristics of the XSLT language (the use of XML syntax, the principle of no side-effects, and the rule-based processing model) that were essential features of XSLT 1.0 and that have been retained essentially unchanged in XSLT 2.0. The fourth characteristic is new in XSLT 2.0, and creates a fundamental change in the nature of XSLT as a language. This is the adoption of a type system based on XML Schema.
There are two aspects to the type system of any programming language. The first is the set of types that are supported (for example, integers, strings, lists, tuples), together with the mechanisms for creating user-defined types. The second aspect is the rules that the language enforces to ensure type-correctness.
XSLT 1.0 had a very small set of types (integers, booleans, strings, node-sets, and result tree fragments), and the rules it applied were what is often called “weak typing”: this means that the processor would always attempt to convert the value supplied in an expression or function call to the type that was required in that context. This makes for a very happy-go-lucky environment: if you supply an integer where a string is expected, or vice versa, nothing will break.
XSLT 2.0 has changed both aspects of the type system. There is now a much richer set of types available (and this set is user-extensible), and the rules for type checking are stricter.
We will look at the implications of this in greater detail in Chapters 4 and 5.
A Two-Language System: XSLT and XPath
The final characteristic of XSLT that I want to explore is its use of XPath as an embedded sublanguage. This is not unlike the way SQL is often embedded in other languages, and it is done for the same reason—it allows XPath to be used in many different contexts.
The fact that XPath was designed to be embedded in other language has some specific consequences:
The syntax of XPath has some unusual features, which reflect the fact that it amalgamates ideas from a number of different sources. One can identify three different syntactic styles within XPath expressions: