Directs the XSL processor to find the appropriate template to apply based on the type and context of each selected node.
Syntax
<xsl:apply-templates order-by="sort-criteria-list" select="pattern" >
Attributes
- order-by
- Sort criteria in a semicolon-separated list. When the first sort results in two equal items, the second sort criterion is checked, and so on. The first non-white-space character in each sort criterion indicates whether the sort is ascending (optional +) or descending (-). The sort criterion is expressed as an XSL pattern, relative to the pattern described in the select attribute.
- select
- Context for which the template should be executed. The default value "node()" indicates selection of the children of current node, of all node types except attributes.
Element Information
Number of occurrences Unlimited Parent elements xsl:attribute, xsl:comment, xsl:copy, xsl:element, xsl:for-each, xsl:if, xsl:otherwise, xsl:pi, xsl:template, xsl:when, output elements Child elements xsl:template Requires closing tag Yes. XSL is an XML grammar and, like all XML grammars, all tags must have closing tags to satisfy the definition of well-formed.
Remarks
The xsl:apply-templates element first selects a set of nodes using the query specified in the select attribute. 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 XSL processor to find an appropriate xsl:template to apply. Templates are tested for applicability by comparing the node to the XSL pattern specified in the template's match attribute. If more than one template satisfies the match pattern, the one appearing last in the stylesheet is chosen.
The xsl:apply-templates element may itself contain xsl:template elements, in which case these templates are tested before templates defined elsewhere. These templates are only available within the xsl:apply-templates and are not visible from outside. This mechanism allows the definition of locally scoped templates.
Note that the order-by attribute syntax is obsolete according to the December XSL Working Draft, which defines the xsl:sort element instead. Future releases will support the official syntax.
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 order-by attribute sorts the customers by state, with all customers from a single state sorted by name.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <HTML> <BODY> <TABLE> <xsl:for-each select="customers/customer" order-by="address/state; name"> <TR> <xsl:apply-templates select="name" /> <xsl:apply-templates select="address" /> <xsl:apply-templates select="phone" /> </TR> </xsl:for-each> </TABLE> </BODY> </HTML> </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="text()"><xsl:value-of /></xsl:template> </xsl:stylesheet>
See Also