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>
<xsl:template>
element has a name
attribute, it can, but need not, also have a match
attribute.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.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 namespace 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.
<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.Number of occurrences | Unlimited |
Parent elements | xsl:stylesheet, xsl:transform |
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"
:
Note To test this example in Internet Explorer, you need to use a script. For more information, see Initiate XSLT in a Script.
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