Reusing Templates - Exercise

Contact Us or call 1-877-932-8228
Reusing Templates - Exercise

Reusing Templates

Duration: 15 to 20 minutes.

In this exercise, you will practice reusing templates.

  1. Open MultipleDocs/Exercises/HTML.xsl and review the document.
  2. Open MultipleDocs/Exercises/CourseAsList.xsl for editing.
  3. Modify this document so that it imports or includes HTML.xsl.
  4. Notice that the lines below are not in the StartHTML template in CourseAsList.xsl. You will need to create another template that holds these lines.
    <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"/>
  5. To test your solution, transform MultipleDocs/Exercises/XML101.xml against MultipleDocs/Exercises/CourseAsList.xsl.

Solution:

MultipleDocs/Solutions/CourseAsList.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="HTML.xsl"/>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
	<xsl:call-template name="StartHTML">
		<xsl:with-param  name="Title" select="course/head/title"/>
	</xsl:call-template>
</xsl:template>

<xsl:template match="course">
	<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"/>
</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>
Next