<xsl:template> Element
Defines a reusable template for generating the desired output for nodes of a particular type and context.
<xsl:template name= Qname match = Pattern priority = number mode = QName </xsl:template>
- name
-
The Qualified Names to be expanded. If it has a prefix, the prefix is expanded into a Uniform Resource Identifier (URI) reference using the namespace 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 namespace is not used for unprefixed names. If an
<xsl:template>element has anameattribute, it can, but need not, also have amatchattribute.
- match
-
A Patterns that identifies the source node or nodes to which the rule applies. The
matchattribute is required unless the<xsl:template>element has anameattribute. The content of the<xsl:template>element is the template that is instantiated when the template rule is applied.
- priority
-
The priority number for the template. 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.
-
If the pattern contains multiple alternatives separated by |, it is treated equivalently to a set of template rules, one for each alternative.
- mode
-
The mode value. This value allows an element to be processed multiple times, each time producing a different result. If
<xsl:template>does not have amatchattribute, it must not have amodeattribute. If an<xsl:apply-templates>element has amodeattribute, it applies only to those template rules from<xsl:template>elements that have amodeattribute with the same value; if an<xsl:apply-templates>element does not have amodeattribute, it applies only to those template rules from<xsl:template>elements that do not have amodeattribute.
|
Number of occurrences |
Unlimited |
|
Parent elements |
|
|
Child elements |
xsl:apply-imports,xsl:apply-templates, xsl:attribute, xsl:call-template, xsl:choose, xsl:comment, xsl:copy, xsl:copy-of, xsl:element, xsl:fallback, xsl:for-each, xsl:if, xsl:message, xsl:number, xsl:param, xsl:processing-instruction, xsl:text, xsl:value-of, xsl:variable, output elements |
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 namespace 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 namespace is not used for unprefixed names.
If an <xsl:template> element has a name attribute, it can, 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.
This template rule has a pattern that identifies <stock> elements and produces an output <DIV> element with the attribute STYLE="font-weight:bold":
XML File (portfolio.xml)
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="templ.xsl"?> <portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes" xml:space="preserve"> <stock exchange="nyse"> <name>zacx corp</name> <symbol>ZCXM</symbol> <price dt:dt="number">28.875</price> </stock> <stock exchange="nasdaq"> <name>zaffymat inc</name> <symbol>ZFFX</symbol> <price dt:dt="number">92.250</price> </stock> <stock exchange="nasdaq"> <name>zysmergy inc</name> <symbol>ZYSZ</symbol> <price dt:dt="number">20.313</price> </stock> </portfolio>
XSLT File (templ.xsl)
<?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="stock"> <DIV STYLE="font-weight:bold"> Symbol: <xsl:value-of select="symbol" />, Price: <xsl:value-of select="price" /> </DIV> </xsl:template> </xsl:stylesheet>
Output
This is the formatted output:
Symbol: ZCXM, Price: 28.875
Symbol: ZFFX, Price: 92.250
Symbol: ZYSZ, Price: 20.313
Build Date: