XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / Basic stylesheet structure: xsl:template
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 1.)
The content of xsl:stylesheet forms the body of the actual XSLT stylesheet. Immediately inside the root are further instructions, called top-level elements because they occupy the stylesheet’s highest level.
Element xsl:template and its contents form the essential basic structure, a template. This term is not yet precise enough and will shortly be refined. For now, each XSLT template is responsible for generating part of the result.
A stylesheet normally contains several templates, executed in succession, sometimes repeatedly, to build the result incrementally.
Rules for invoking templates
Although a template essentially outputs the same result, the document’s structure depends on which templates run, in what order and how often. Invocation must therefore follow rules.
Define the rule using attribute match. The terms rule and template are nearly synonymous: an xsl:template element with attribute match is officially called a template rule. (Note: An attribute match may instead be replaced by attribute name. This is a named template, not a template rule.) The term covers the whole template element and its contents. I will occasionally use the less strict term template.
The match pattern: an XPath pattern
The pattern governing execution is written in XPath, hence is an XPath pattern. Slash “/” is one of the simplest patterns:
<xsl:template match="/">
<!-- der Inhalt der Template-Regel -->
</xsl:template>
The rule’s content, above only a comment, is its template body. It executes whenever the pattern in attribute match matches a source-document part currently awaiting processing.
A name or context test can activate the pattern for elements with a particular name or context. The slash “/” here does not name a tag but, in a sense, the source document itself. The rule therefore activates whenever a source document exists, normally always.
Execution is called instantiation of the template rule, resembling a function call performing fixed work. Each match with a source part awaiting processing adds the activated rule’s Literal Result Elements to the result, eventually the output document if transformation succeeds, and processes its XSLT instructions. The latter can produce different results on each instantiation.
Summary of xsl:template
A template element
xsl:templatewith attributematchis called a template rule.Attribute
match, from to match, contains a match pattern (pattern) determining when the rule executes, also called instantiation.XPath pattern "
/" matches the input document itself, more precisely the document node representing it, as explained later. Such a source document is required. This rule is therefore always invoked once at the start of processing.
Next, examine the template body: Literal Result Elements and XSLT instructions.
Figure: Structure of a template rule.
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