SAXONICA

Saxon Documentation
Full Contents
About Saxon
Changes in this Release
Conditions of Use
Using XSLT 2.0
Using XQuery
Handling Source Documents
XML Schema Processing
XPath API for Java
Saxon on .NET
Extensibility
Saxon Extensions
Sample Saxon Applications
The Saxon SQL Extension
XSLT Elements
| Introduction
| xsl:analyze-string
| xsl:apply-imports
| xsl:apply-templates
| xsl:attribute
| xsl:attribute-set
| xsl:call-template
| xsl:character-map
| xsl:choose
| xsl:comment
| xsl:copy
| xsl:copy-of
| xsl:decimal-format
| xsl:document
| xsl:element
| xsl:fallback
| xsl:for-each
| xsl:for-each-group
| xsl:function
| xsl:if
| xsl:include
| xsl:import
| xsl:import-schema
| xsl:key
| xsl:matching-substring
| xsl:message
| xsl:namespace
| xsl:namespace-alias
| xsl:next-match
| xsl:non-matching-substring
| xsl:number
| xsl:otherwise
| xsl:output
| xsl:output-character
| xsl:param
| xsl:perform-sort
| xsl:preserve-space
| xsl:processing-instruction
| xsl:result-document
| xsl:sequence
| xsl:sort
| xsl:strip-space
| xsl:stylesheet
» | xsl:template
| xsl:text
| xsl:value-of
| xsl:variable
| xsl:when
| xsl:with-param
| Literal Result Elements
| XSLT Patterns
XPath 2.0 Expression Syntax
Function Library
Standards Conformance

# xsl:template

The `xsl:template` element defines a processing rule for source elements or other nodes of a particular type.

The type of node to be processed is identified by a pattern, written in the mandatory `match` attribute. The most common form of pattern is simply an element name. However, more complex patterns may also be used: The syntax of patterns is given in more detail in XSLT Pattern Syntax

The following examples show some of the possibilities:

Pattern | Meaning
XXX | Matches any element whose name (tag) is XXX
* | Matches any element
XXX/YYY | Matches any YYY element whose parent is an XXX
XXX//YYY | Matches any YYY element that has an ancestor named XXX
/*/XXX | Matches any XXX element that is immediately below the root (document) element
*[@ID] | Matches any element with an ID attribute
XXX[1] | Matches any XXX element that is the first XXX child of its parent element. (Note that this kind of pattern can be very inefficient: it is better to match all XXX elements with a single template, and then use xsl:if to distinguish them)
SECTION[TITLE="Contents"] | Matches any SECTION element whose first TITLE child element has the value "Contents"
A/TITLE | B/TITLE | C/TITLE | Matches any TITLE element whose parent is of type A or B or C
text() | Matches any character data node
@* | Matches any attribute
/ | Matches the document node

The `xsl:template` element has an optional `mode` attribute. If this is present, the template will only be matched when the same mode is used in the invoking `xsl:apply-templates` element. The value can be a list of mode names, indicating that the template matches more than one mode; this list can include the token `#default` to indicate that the template matches the default (unnamed) mode. Alternatively the `mode` attribute can be set to `#all`, to indicate that the template matches all modes. (This can be useful in conjunction with `xsl:next-match`: one can write a template rule that matches in all modes, and then call `xsl:next-match` to continue processing in the original mode.)

There is also an optional `name` attribute. If this is present, the template may be invoked directly using `xsl:call-template`. The match attribute then becomes optional.

If there are several `xsl:template` elements that all match the same node, the one that is chosen is determined by the optional `priority` attribute: the template with highest priority wins. The priority is written as a floating-point number; the default priority is 1. If two matching templates have the same priority, the one that appears last in the stylesheet is used.

Examples:

The following examples illustrate different kinds of template and match pattern.

Example 1: a simple XSLT template for a particular element. This example causes all <ptitle> elements in the source document to be output as HTML <h2> elements.

    <xsl:template match="ptitle">
        <h2>
            <xsl:apply-templates/>
        </h2>
    </xsl:template>

Next