Chapter 3. Templates, Variables, and Parameters

When you are writing a stylesheet, some of the intended output, excepting the case of text, will come from generating elements and attributes that conform to the schema of the resulting document.

However, most of the output content will come from the source tree that the processor has loaded, and will be obtained (as a first step) by matching source tree nodes to patterns in the stylesheet templates.

Templates, specified with the <xsl:template> element, are core structures in XSLT. You use them as containers for the sequences of instructions that select values from the source tree and write them to the result tree. In addition to specifying matching patterns, you can give templates priority values to cover cases when several templates match the same source node. You can also specify different modes of processing for a given match. Named templates can be called explicitly, providing a degree of control over the processing sequence.

In XSLT you can use both global and local variables, defined with the <xsl:variable> element. As in any programming language, they enable you to set or calculate a value once and then use it in many places. They also have other uses, including storing temporary trees of nodes. However, in XSLT variables cannot be updated. XSLT is designed to be free of side effects. A side effect of assigning variable values as you might in other languages is that the processor would have to handle instructions in a particular ...

Get Beginning XSLT and XPath: Transforming XML Documents and Data now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.