Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

Directs the XSL Transformations (XSLT) processor to find the appropriate template to apply, based on the type and context of each selected node.

Syntax

<xsl:apply-templates
  select = 
expression
  mode = 
QName>
</xsl:apply-templates>

Attributes

select

Can be used to process nodes selected by an expression instead of processing all children. The value of the selectattribute is an expression. The expression must evaluate to a node-set. The selected set of nodes is processed in document order unless a sorting specification is present.

mode

The modeattribute allows an element to be processed multiple times, each time producing a different result. If <xsl:template> does not have a matchattribute, it must not have a modeattribute. If an <xsl:apply-templates> element has a modeattribute, it applies only to those template rules from <xsl:template> elements that have a modeattribute with the same value; if an <xsl:apply-templates> element does not have a modeattribute, it applies only to those template rules from <xsl:template> elements that do not have a modeattribute.

Element Information

Remarks

The <xsl:apply-templates> element first selects a set of nodes using the query specified in the selectattribute. If this attribute is left unspecified, all children of the current node are selected. For each of the selected nodes, <xsl:apply-templates> directs the XSLT processor to find an appropriate <xsl:template> to apply. Templates are tested for applicability by comparing the node to the XPath expression specified in the template's matchattribute. If more than one template satisfies the match pattern, the one appearing with the highest priority is chosen. If several templates have the same priority, the last in the style sheet is chosen.

Example

The following style sheet formats customer data in XML into an HTML <TABLE>element, where each row represents a customer and the columns represent the customer's name, address, and phone number. The <xsl:sort>element sorts the customers by state, with all customers from a single state sorted by name.

Copy Code
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:template match="/">
	<HTML>
	<BODY>
		<TABLE>
		<xsl:apply-templates select="customers/customer">
			<xsl:sort select="state"/>
			<xsl:sort select="name"/> 
		</xsl:apply-templates>
		</TABLE>
	</BODY>
	</HTML>
  </xsl:template>
  <xsl:template match="customer">
	 <TR>
		<xsl:apply-templates select="name" />
		<xsl:apply-templates select="address" />
		<xsl:apply-templates select="phone" />
		<xsl:apply-templates select="phone"
mode="accountNumber"/>
	</TR>
  </xsl:template>
  <xsl:template match="name">
	<TD STYLE="font-size:14pt font-family:serif">
	<xsl:apply-templates />
	</TD>
  </xsl:template>
  <xsl:template match="address">
	<TD> <xsl:apply-templates /> </TD>
  </xsl:template>
  <xsl:template match="phone">
	<TD> <xsl:apply-templates /> </TD>
  </xsl:template>
  <xsl:template match="phone" mode="accountNumber">
	<TD STYLE="font-style:italic">
	1-<xsl:value-of select="."/>-001
	</TD>
  </xsl:template>
</xsl:stylesheet>

See Also