Variables and named templates

A stylesheet often contains a number of templates that produce identical or very similar output. XSLT includes some mechanisms for avoiding such redundancy.


A 'variable' is a container of information that has a name. Whenever the variable is referenced in the stylesheet, the reference is replaced by the value of the variable.

Variables are declared using the Variable element. The name of the variable is given in the Name attribute, and the value of the variable may be the content of the element. The following example creates a variable called 'Colour', and gives it the value 'red':

<variable name="Colour">red</variable>

Alternatively, the value can be generated from an expression using a Select attribute. ...

Get XML Companion, The, Third Edition 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.