4. Templates

XSLT is not a programming language in which all the commands are simply executed one after the other. An XSLT stylesheet can be seen as a collection of scenarios which will only be carried out in the case of a certain event. Each scenario is described in a <template> element. They are all given directly under the <stylesheet> root. A tree diagram of an XSLT stylesheet would look as follows:




Each <template> is in turn a collection of instructions. To activate a specific template, we must make use of the match attribute of the <template> element. The value of this attribute is normally the name of one of the elements in the XML source tree. At the start of the transformation, the XSLT processor will compare the elements in the source tree with the element names that mentioned in the match attributes in the various templates. If a match is found, that template will be invoked and its instructions will be carried out.





To make sure that the transformation is carried out in a controlled manner, there must always be one template with a match attribute that points either to the document root or the root node. The document root is the very first element in the document that encapsulates all the other elements in the XML file. If we point to the document root of the XML file collection.xml, the first template will look as follows:

<template match="collection">
...
</template>

The root node, on the other hand, is represented by a forward slash (“/”). If we choose to refer to this point, the first template will look as follows:

<template match="/">
...
</template>

In this tutorial, we shall choose the first option.