Accessing Nodes - Exercise

Welcome to our free XML tutorial. This tutorial is based on Webucator's Introduction to XML Training course.

Contact Us or call 1-877-932-8228
Accessing Nodes - Exercise

Accessing Nodes

Duration: 25 to 35 minutes.

In this exercise, you will practice using XPath by modifying the XSLT used to transform the XML document below.

Code Sample:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="XPaths.xsl"?>
		<SendDate>November 29, 2005</SendDate>
			<Name Title="Mr.">
			<Company>Lockwood &amp; Lockwood</Company>
				<Street>291 Broadway Ave.</Street>
				<City>New York</City>
				<Country>United States</Country>
				Along with this letter, I have enclosed the following items:
				two original, execution copies of the Webucator 
				Master Services Agreement
				two original, execution copies of the Webucator Premier Support for 
				Developers Services Description between 
				Lockwood &amp; Lockwood and Webucator, Inc.
		<Para>Please sign and return all four original, execution copies to me at
		your earliest convenience.  Upon receipt of the executed copies, we will
		immediately return a fully executed, original copy of both agreements
		to you.</Para>
	Please send all four original execution copies to my attention as follows:

					<Company>Webucator, Inc.</Company>
					<Street>4933 Jamesville Rd.</Street>
		<Para>If you have any questions, feel free to call me at 
		<Phone>800-555-1000 x123</Phone> or e-mail me at 
			<JobTitle>VP of Operations</JobTitle>

Please follow these steps.

  1. Open XPath/Exercises/XPaths.xsl for editing.
  2. This file contains three xsl:templates, one matching Head, Body, and Foot. In each template is a comment showing what the goal output is. You will use xsl:value-of tags and XPath to create this output from the XML file shown above.
  3. You may need to use some other XSL tags, such as the xsl:text tag.
  4. To test your solutions, transform XPath/Exercises/BusinessLetter.xml against XPath/Exercises/XPaths.xsl.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
			<xsl:apply-templates />
	<xsl:template match="Head">
			Mr. Joshua Lockwood is from the United States-->
			<xsl:value-of select="child::Recipient/child::Name/attribute::Title"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="child::Recipient/child::Name/child::FirstName"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="child::Recipient/child::Name/child::LastName"/> 
			is from the 
			<xsl:value-of select="child::Recipient/child::Address/child::Country"/>
	<xsl:template match="Body">
			Bill Smith works at Webucator, Inc.
			His email is
			If you have any questions, feel free to call me at 800-555-1000 x123.-->
			<xsl:value-of select="descendant::FirstName"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="descendant::LastName"/> works at 
			<xsl:value-of select="descendant::Company"/>
			His email is <xsl:value-of select="child::Para/child::Email"/>
					select="child::Para[position() = last()]/text()[position()=1]"/>
			<xsl:value-of select="child::Para/child::Phone"/>.
	<xsl:template match="Foot">
			VP of Operations: Smith, Bill -->
			<xsl:value-of select="descendant::JobTitle"/>:
			 <xsl:value-of select="descendant::LastName"/>, 
			 <xsl:value-of select="descendant::FirstName"/>

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