Share via


<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>

Attributes

  • 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 a name attribute, it can, but need not, also have a match attribute.
  • match
    A Patterns 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
    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.

    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.

  • 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 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

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

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 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.

Example

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