Accessing Nodes - Exercise

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:

XPath/Exercises/BusinessLetter.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="XPaths.xsl"?>
<BusinessLetter>
	<Head>
		<SendDate>November 29, 2005</SendDate>
		<Recipient>
			<Name Title="Mr.">
				<FirstName>Joshua</FirstName>
				<LastName>Lockwood</LastName>
			</Name>
			<Company>Lockwood &amp; Lockwood</Company>
			<Address>
				<Street>291 Broadway Ave.</Street>
				<City>New York</City>
				<State>NY</State>
				<Zip>10007</Zip>
				<Country>United States</Country>
			</Address>
		</Recipient>
	</Head>
	<Body>
		<List>
			<Heading>
				Along with this letter, I have enclosed the following items:
			</Heading>
			<ListItem>
				two original, execution copies of the Webucator 
				Master Services Agreement
			</ListItem>
			<ListItem>
				two original, execution copies of the Webucator Premier Support for 
				Developers Services Description between 
				Lockwood &amp; Lockwood and Webucator, Inc.
			</ListItem>
		</List>
		<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>
		<Para>
	Please send all four original execution copies to my attention as follows:

	<Person>
				<Name>
					<FirstName>Bill</FirstName>
					<LastName>Smith</LastName>
				</Name>
				<Address>
					<Company>Webucator, Inc.</Company>
					<Street>4933 Jamesville Rd.</Street>
					<City>Jamesville</City>
					<State>NY</State>
					<Zip>13078</Zip>
					<Country>USA</Country>
				</Address>
			</Person>
		</Para>
		<Para>If you have any questions, feel free to call me at 
		<Phone>800-555-1000 x123</Phone> or e-mail me at 
		<Email>bsmith@webucator.com</Email>.</Para>
	</Body>
	<Foot>
		<Closing>
			<Name>
				<FirstName>Bill</FirstName>
				<LastName>Smith</LastName>
			</Name>
			<JobTitle>VP of Operations</JobTitle>
		</Closing>
	</Foot>
</BusinessLetter>

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.

Solution:

XPath/Solutions/XPaths.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
			xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
		<XPathTests>
			<xsl:apply-templates />
		</XPathTests>
	</xsl:template>
	<xsl:template match="Head">
		<XPathTest>
			<!--OUTPUT: 
			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"/>
		</XPathTest>
	</xsl:template>
	<xsl:template match="Body">
		<XPathTest>
			<!--OUTPUT:
			Bill Smith works at Webucator, Inc.
			His email is bsmith@webucator.com.
			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"/>
			<xsl:text>
			</xsl:text>
			<xsl:value-of 
					select="child::Para[position() = last()]/text()[position()=1]"/>
			<xsl:value-of select="child::Para/child::Phone"/>.
		</XPathTest>
	</xsl:template>
	<xsl:template match="Foot">
		<XPathTest>
			<!--OUTPUT:
			VP of Operations: Smith, Bill -->
			<xsl:value-of select="descendant::JobTitle"/>:
			 <xsl:value-of select="descendant::LastName"/>, 
			 <xsl:value-of select="descendant::FirstName"/>
		</XPathTest>
	</xsl:template>
</xsl:stylesheet>
Next