XML & XSL - xsl:apply-templates


Applies a template to all or part of an XML document sometimes based on a pattern match.

Syntax

<xsl:apply-templates order-by="sort-criteria-list" select="pattern">

Attributes

Parent Elements

Child Elements

Purpose

xsl:apply-templates 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 occuring last in the stylesheet is selected; each successive template over-rides the previously declared one.

If the xsl:apply-templates element contains xsl:template elements these templates are tested first. These templates are locally scoped within xsl:apply-templates.

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.

Customers Example

XML Data Tree Structure

Click here to see the XML structure of this example.

XML & XSL in Action

Click here to see the XSL formatted display of this example.

The XSL Style Sheet

<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 BGCOLOR="#FFF0F0" STYLE="color:red; font-weight:bold; font-size:14pt; font-family:serif;"><xsl:apply-templates/></TD>
	</xsl:template>
	
	<xsl:template match="address">
		<TD BGCOLOR="#F0FFF0" STYLE="color:green;">
			<xsl:value-of select="street"/>, 
			<xsl:value-of select="town"/>, 
			<xsl:value-of select="state"/>, 
			<xsl:value-of select="zip"/>
				<xsl:apply-templates/>
		</TD>
	</xsl:template>
	
	<xsl:template match="phone">
		<TD BGCOLOR="#F0FFFF" STYLE="color:blue;"><xsl:apply-templates/></TD>
	</xsl:template>

	<xsl:template match="text()"><xsl:value-of/></xsl:template>

</xsl:stylesheet>

HTML Equivalent

<HTML>
<BODY>

	<TABLE>

		<TR>
			<TD BGCOLOR="#FFF0F0"><FONT COLOR="red><B>Jim Jones</B></FONT></TD>
			<TD BGCOLOR="#F0FFF0"><FONT COLOR="green>PO Box 3261, NY, NY, 11001</FONT></TD>
			<TD BGCOLOR="#F0FFFF"><FONT COLOR="blue">631-123-4567</FONT></TD>
		</TR>

		<TR>
			<TD BGCOLOR="#FFF0F0"><FONT COLOR="red><B>James Bloggs</B></FONT></TD>
			<TD BGCOLOR="#F0FFF0"><FONT COLOR="green>25 5th Street, Manhattan, NY, 11124</FONT></TD>
			<TD BGCOLOR="#F0FFFF"><FONT COLOR="blue">212-123-5566</FONT></TD>
		</TR>

		<TR>
			<TD BGCOLOR="#FFF0F0"><FONT COLOR="red><B>Jim Jones</B></FONT>>>>></TD>
			<TD BGCOLOR="#F0FFF0"><FONT COLOR="green>25 Amery Street, Jones Beach, NY, 11744</FONT></TD>
			<TD BGCOLOR="#F0FFFF"><FONT COLOR="blue">516-456-5467</FONT></TD>
		</TR>

		<TR>
			<TD BGCOLOR="#FFF0F0"><FONT COLOR="red><B>Zachary Smith</B></FONT></TD>
			<TD BGCOLOR="#F0FFF0"><FONT COLOR="green>1 Smith Street, Smithtown, NY, 11723</FONT></TD>
			<TD BGCOLOR="#F0FFFF"><FONT COLOR="blue">631-445-2231</FONT></TD>
		</TR>

	</TABLE>

</BODY>
</HTML>