xsl:apply-templates

Welcome to our free XSLT 1.0 and XPath 1.0 tutorial. This tutorial is based on Webucator's XSLT 1.0 and XPath 1.0 Training course.

Contact Us or call 1-877-932-8228
xsl:apply-templates

xsl:apply-templates

Most XSLT documents contain more than one <xsl:template> tag. The <xsl:apply-templates /> tag is used to indicate that the XSLT processor should look deeper into the XML document and, at each step, check the XSLT document to see if there is a matching template for the current element.

The following is a simple course outline marked up as XML.

Code Sample:

TemplatesParametersVariables/Demos/Xml101.xml
<?xml version="1.0"?>
<?xml-stylesheet href="CourseAsList.xsl" type="text/xsl"?>
<course>
	<head>
		<title>Introduction to XML</title>
		<course_num>XML101</course_num>
		<course_length>3 days</course_length>
	</head>
	<body>
		<prerequisites>
			<prereq>Experience with Word Processing</prereq>
			<prereq optional="true">Experience with HTML</prereq>
			<prereq>Experience with computers</prereq>
		</prerequisites>
		<outline>
			<topics>
				<title>XML Basics</title>
				<topics>
					<title>What is XML?</title>
					<title>XML Benefits</title>
					<topics>
						<title>XML Holds Data, Nothing More</title>
						<title>XML Separates Structure from Formatting</title>
						<title>XML Promotes Data Sharing</title>
						<title>XML is Human-Readable</title>
						<title>XML is Free</title>
					</topics>
				</topics>
				<topics>
					<title>XML Documents</title>
					<topics>
						<title>The Prolog</title>
						<title>Elements</title>
						<title>Attributes</title>
						<title>CDATA</title>
						<title>XML Syntax Rules</title>
						<title>Special Characters</title>
					</topics>
				</topics>
				<topics>
					<title>Creating a Simple XML File</title>
				</topics>
			</topics>
		</outline>
	</body>
	<foot>
		<creator>Josh Lockwood</creator>
		<date_created>2011-07-25</date_created>
		<modifications madeby="Colby Germond" date="2011-05-05">
			<modification type="insert">Added HTML prerequisite</modification>
			<modification type="edit">Fixed some typos</modification>
		</modifications>
		<modifications madeby="Ted Ferris" date="2011-05-05">
			<modification type="insert">Added prerequisite: Experience with 
				Computers</modification>
			<modification type="insert">Added XML Documents and Creating
				a Simple XML Document Topics</modification>
		</modifications>
	</foot>
</course>

Notice the xml-stylesheet processing instruction pointing to CourseAsList.xsl. Let's look at that file.

Code Sample:

TemplatesParametersVariables/Demos/CourseAsList.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" indent="yes"/>
<xsl:template match="/">
	<html>
		<head>
			<title><xsl:value-of select="/course/head/title"/></title>
			<style type="text/css">
				#outline { font-size:larger }
				ul ul {font-size: smaller }
			</style>
		</head>
		<body>
		<h1><xsl:value-of select="/course/head/title"/></h1>
		<div id="courseNum"><xsl:value-of select="/course/head/course_num"/></div>
		<div id="courseLength">
			<xsl:value-of select="/course/head/course_length"/>
		</div>
		<xsl:apply-templates select="/course/body/prerequisites"/>
		<xsl:apply-templates select="/course/body/outline"/>
		</body>
	</html>
</xsl:template>

<xsl:template match="prerequisites">
	<h2>Prerequisites</h2>
	<ul>
	<xsl:for-each select="prereq">
		<li>
			<xsl:value-of select="." />
			<xsl:if test="@optional = 'true'">
				(optional, but recommended)
			</xsl:if>
		</li>
	</xsl:for-each>
	</ul>
</xsl:template>

<xsl:template match="outline">
	<h2>Course Outline</h2>
	<div id="outline">
		<xsl:apply-templates />
	</div>
</xsl:template>

<xsl:template match="topics">
	<ul>
		<xsl:apply-templates />
	</ul>
</xsl:template>

<xsl:template match="title">
	<li><xsl:apply-templates /></li>
</xsl:template>

</xsl:stylesheet>

As you can see, this file uses multiple <xsl:template> tags to match different elements in the XML file. Each time one of the specified elements is matched, the matching template comes into play. The <xsl:apply-templates /> tags with no select attribute tell the XSLT processor to continue looking through the XML document for any node matched in a template.

If an element found in the source document has no matching template, the default XSLT template, which simply outputs the text content of the element, is used.

Next

This tutorial is based on Webucator's XSLT 1.0 and XPath 1.0 Training Course. We also offer many other XML Training courses. Sign up today to get help from a live instructor.