xsl:template Element
A version of this page is also available for
4/8/2010
Defines a re-useable template for generating the desired output for nodes of a particular type and context.
Syntax
<xsl:template
name = QName
match = pattern
priority = "number"
mode = QName
</xsl:template>
Attributes
- name
The value of the name attribute is a Qname that is expanded. If it has a prefix, the prefix is expanded into a Uniform Resource Identifier (URI) reference using the name space declarations in effect on the attribute in which the name occurs. The expanded-name, consisting of the local part of the name and the possibly null URI reference is used as the name of the template. The default name space is not used for non-prefixed names. If an <xsl:template> element has a name attribute, it may, but need not, also have a match attribute.
- match
A pattern that identifies the source node or nodes to which the rule applies. The match attribute is required unless the <xsl:template> element has a name attribute. The content of the <xsl:template> element is the template that is instantiated when the template rule is applied.
priority
All matching template rules that have lower priority than the matching template rule or rules with the highest priority are eliminated from consideration. The value of this must be a real number from 0–9, positive or negative, matching the production number with an optional leading minus sign (-). The default priority is computed as follows:- If the pattern contains multiple alternatives separated by |, it is treated equivalently to a set of template rules, one for each alternative.
- If the pattern has the form of a Qname preceded by a child or attribute axis specifier or has the form processing-instruction literal preceded by a child or attribute axis specifier, the priority is 0.
- If the pattern is a name preceded by a child or attribute axis specifier, the priority is -0.25.
- Otherwise, if the pattern consists of just a node test preceded by a child or attribute axis specifier, the priority is -0.5.
- Otherwise, the priority is 0.5.
Thus the most common kind of pattern (a pattern that tests for a node with a particular type and a particular expanded-name) has priority 0. The next less specific kind of pattern (a pattern that tests for a node with a particular type and an expanded-name with a particular name space URI) has priority -0.25. Patterns less specific than this (patterns that just test for nodes with particular types) have priority -0.5. Patterns more specific than the most common kind of pattern have priority 0.5.
- mode
The mode attribute allows an element to be processed multiple times, each time producing a different result. If <xsl:template> does not have a match attribute, it must not have a mode attribute. If an <xsl:apply-templates> element has a mode attribute, it applies only to those template rules from <xsl:template> elements that have a mode attribute with the same value; if an <xsl:apply-templates> element does not have a mode attribute, it applies only to those template rules from <xsl:template> elements that do not have a mode attribute.
Element Information
Number of occurrences |
Unlimited |
Parent elements |
|
Child elements |
xsl:apply-templates, xsl:attribute, xsl:call-template, xsl:choose, xsl:comment, xsl:copy, xsl:copy-of, xsl:element, xsl:for-each, xsl:if, xsl:param, xsl:processing-instruction, xsl:text, xsl:value-of, xsl:variable, output elements |
Remarks
Note that the template need not generate a complete XML document (even the root template, unless using transformNodeToObject), but only a fragment of XML. It is possible to include unenclosed text or multiple document elements defined by the template. This facilitates the generation of raw text and XML fragments that can be further processed by an application (for example, HTML fragments inserted into an HTML page).
The value of the name attribute is a Qname that is expanded. If it has a prefix, it is expanded into a URI reference using the name space declarations in effect on the attribute in which the name occurs. The expanded-name consisting of the local part of the name and the possibly null URI reference is used as the name of the template. The default name space is not used for non-prefixed names.
If an <xsl:template> element has a name attribute, it may, but need not, also have a match attribute. An <xsl:call-template> element invokes a template by name; it has a required name attribute that identifies the template to be invoked. Unlike <xsl:apply-templates>, <xsl:call-template> does not change the current node or the current node list.
An error occurs if a style sheet contains more than one template with the same name.
Example
The following template (in this case defined by the <xsl:template> element) has a pattern that identifies elements of type "stock" and produces an output <DIV> element with the attribute STYLE="font-weight:bold":
<xsl:template match="stock">
<DIV STYLE="font-weight:bold">
Symbol: <xsl:value-of match="symbol" />, Price: <xsl:value-of match="price" />
</DIV>
</xsl:template>