Using xsl:call-template - Exercise

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
Using xsl:call-template - Exercise

Using xsl:call-template

Duration: 10 to 15 minutes.

In this exercise, you will practice passing parameters to named templates.

  1. Open TemplatesParametersVariables/Exercises/CourseAsList.xsl.
  2. Below the first template, which matches the root, create a new template name StartHTML.
  3. Move the entire contents of the first template into the new template you just created.
  4. Add an xsl:call-template element to the first template that calls the StartHTML template and passes it the value of the title element from the source XML document.
  5. Modify the StartHTML template so that it accepts the passed-in parameter and uses it in the nested HTML title and h1 elements.
  6. To test your solution, transform TemplatesParametersVariables/Exercises/XML101.xml against TemplatesParametersVariables/Exercises/CourseAsList.xsl.


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
<xsl:output method="html"/>
<xsl:template match="/">
	<xsl:call-template name="StartHTML">
		<xsl:with-param  name="Title" select="course/head/title"/>

<xsl:template name="StartHTML">
	<xsl:param name="Title" select="'Untitled'"/>
			<title><xsl:value-of select="$Title"/></title>
			<style type="text/css">
				#outline { font-size:larger }
				ul ul {font-size: smaller }
		<h1><xsl:value-of select="$Title"/></h1>
		<div id="courseNum">
			<xsl:value-of select="/course/head/course_num"/>
		<div id="courseLength">
			<xsl:value-of select="/course/head/course_length"/>
		<xsl:apply-templates select="/course/body/prerequisites"/>
		<xsl:apply-templates select="/course/body/outline"/>

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

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

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

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


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.