Transforming the Business Letter - Exercise

Contact Us or call 1-877-932-8228
Transforming the Business Letter - Exercise

Transforming the Business Letter

Duration: 60 to 90 minutes.

This exercise has two parts.

  1. First, you will transform the TyingItTogether/Exercises/BusinessLetter.xml to an HTML document. You can make it look however you like.
  2. Second, you will transform TyingItTogether/Exercises/BusinessLetter2.xml into XML that's valid against TyingItTogether/Exercises/BusinessLetter.dtd, so that you can then apply the XSLT you created in step 1 to output the transformed TyingItTogether/Exercises/BusinessLetter2.xml.

Code Sample:

TyingItTogetherXsl/Exercises/BusinessLetter.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE BusinessLetter SYSTEM "BusinessLetter.dtd">
<?xml-stylesheet type="text/xsl" href="BusinessLetter.xsl"?>
<BusinessLetter>
	<Head>
		<SendDate>November 29, 2011</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, Operations</JobTitle>
		</Closing>
	</Foot>
</BusinessLetter>

Code Sample:

TyingItTogetherXsl/Exercises/BusinessLetter2.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Letter2Letter.xsl"?>
<Letter>
	<Date>November 29, 2011</Date>
	<To>
		<Name>
			<Title>Mr.</Title>
			<FName>Joshua</FName>
			<LName>Lockwood</LName>
		</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>
	</To>
	<List>
		<ListHead>
			Along with this letter, I have enclosed the following items:
		</ListHead>
		<LI>two original, execution copies of the Webucator Master 
			Services Agreement</LI>
		<LI>two original, execution copies of the Webucator Premier 
			Support for Developers Services Description between Lockwood 
				&amp; Lockwood and Webucator, Inc.</LI>
	</List>
	<P>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.</P>
	<P>Please send all four original, execution copies to my attention as 
		follows:
			<Person>
			<Name>
				<FName>Bill </FName>
				<LName>Smith</LName>
			</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>
	</P>
	<P>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>.</P>
	<End>
		<Name>
			<FName>Bill </FName>
			<LName>Smith</LName>
		</Name>
		<Title>VP, Operations</Title>
	</End>
</Letter>

Code Sample:

TyingItTogetherXsl/Exercises/BusinessLetter.dtd
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT Address (Company?, Street, City, State, Zip, Country)>
<!ELEMENT Body (List, Para+)>
<!ELEMENT BusinessLetter (Head, Body, Foot)>
<!ELEMENT City (#PCDATA)>
<!ELEMENT Closing (Name, JobTitle)>
<!ELEMENT Company (#PCDATA)>
<!ELEMENT Country (#PCDATA)>
<!ELEMENT Email (#PCDATA)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT Foot (Closing)>
<!ELEMENT Head (SendDate, Recipient)>
<!ELEMENT Heading (#PCDATA)>
<!ELEMENT JobTitle (#PCDATA)>
<!ELEMENT LastName (#PCDATA)>
<!ELEMENT List (Heading, ListItem+)>
<!ELEMENT ListItem (#PCDATA)>
<!ELEMENT Name (FirstName, LastName)>
<!ATTLIST Name
	Title CDATA #IMPLIED
>
<!ELEMENT Para (#PCDATA | Person | Email | Phone)*>
<!ELEMENT Person (Name, Address)>
<!ELEMENT Phone (#PCDATA)>
<!ELEMENT Recipient (Name, Company, Address)>
<!ELEMENT SendDate (#PCDATA)>
<!ELEMENT State (#PCDATA)>
<!ELEMENT Street (#PCDATA)>
<!ELEMENT Zip (#PCDATA)>

Solution:

TyingItTogetherXsl/Solutions/Letter2HTML.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="html"/>
	<xsl:template match="/BusinessLetter">
		<html>
			<head>
				<title>Business Letter</title>
			</head>
			<body>
				<div style="margin-bottom:10px;">
					<xsl:value-of select="Head/SendDate"/>
				</div>
				<xsl:apply-templates select="Head/Recipient"/>	
				<div style="margin-top:10px">
					Dear <xsl:value-of select="Head/Recipient/Name/@Title"/>
					<xsl:text> </xsl:text>
					<xsl:value-of select="Head/Recipient/Name/LastName"/>:
				</div>
				<xsl:apply-templates select="Body"/>
				<xsl:apply-templates select="Foot"/>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="Recipient | Person">
		<div>
			<xsl:if test="name() = 'Person'">
				<xsl:attribute name="style">margin-left:100px;</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates select="Name"/>
			<xsl:choose>
				<xsl:when test="name() = 'Person'">
					<div><xsl:value-of select="Address/Company"/></div>
				</xsl:when>
				<xsl:otherwise>
					<div><xsl:value-of select="Company"/></div>
				</xsl:otherwise>
			</xsl:choose>
			<div><xsl:value-of select="Address/Street"/></div>
			<div>
				<xsl:value-of select="Address/City"/>,
				<xsl:text> </xsl:text>
				<xsl:value-of select="Address/State"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="Address/Zip"/>
			</div>
			<div><xsl:value-of select="Address/Country"/></div>
		</div>
	</xsl:template>
	<xsl:template match="Body">
		<div style="margin-top:10px; margin-bottom:10px;">
			<xsl:apply-templates/>
		</div>
	</xsl:template>
	<xsl:template match="Para">
		<p><xsl:apply-templates/></p>
	</xsl:template>
	<xsl:template match="List">
		<ul><xsl:apply-templates/></ul>
	</xsl:template>
	<xsl:template match="ListItem">
		<li><xsl:apply-templates/></li>
	</xsl:template>
	<xsl:template match="Heading">
		<div style="font-weight:bold; margin-left:-15px;">
			<xsl:apply-templates/>
		</div>
	</xsl:template>
	<xsl:template match="Email">
		<a href="{.}"><xsl:value-of select="."/></a>
	</xsl:template>
	<xsl:template match="Phone">
		<span style="text-decoration:underline">
			<xsl:value-of select="."/>
		</span>
	</xsl:template>
	
	<xsl:template match="Foot">
		<div style="margin-bottom:20px">Sincerly,</div>
		<div>
			<xsl:apply-templates select="Closing/Name"/>
			<xsl:value-of select="Closing/JobTitle"/>
		</div>
	</xsl:template>
	<xsl:template match="Name">
		<div>
			<xsl:value-of select="@Title"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="FirstName"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="LastName"/>
		</div>	
	</xsl:template>
</xsl:stylesheet>

Solution:

TyingItTogetherXsl/Solutions/Letter2Letter.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="/Letter">
		<xsl:processing-instruction name="xml-stylesheet">
			type="text/xsl" href="BusinessLetter.xsl"
		</xsl:processing-instruction>
		<BusinessLetter>
			<Head>
				<SendDate><xsl:value-of select="Date"/></SendDate>
				<Recipient>
					<xsl:apply-templates select="To"/>
				</Recipient>
			</Head>
			<Body>
				<xsl:apply-templates select="List | P"/>
			</Body>
			<xsl:apply-templates select="End"/>
		</BusinessLetter>
	</xsl:template>
	
	<xsl:template match="To">
		<xsl:apply-templates select="Name"/>
		<xsl:copy-of select="Company"/>
		<xsl:copy-of select="Address"/>
	</xsl:template>
	
	<xsl:template match="List">
		<List>
			<xsl:apply-templates/>
		</List>
	</xsl:template>
	
	<xsl:template match="LI">
		<ListItem>
			<xsl:apply-templates/>
		</ListItem>
	</xsl:template>
	
	<xsl:template match="ListHead">
		<Heading>
			<xsl:apply-templates/>
		</Heading>
	</xsl:template>
	
	<xsl:template match="P">
		<Para>
			<xsl:apply-templates/>
		</Para>
	</xsl:template>
	
	<xsl:template match="Email | Phone">
		<xsl:copy-of select="."/>
	</xsl:template>
	
	<xsl:template match="End">
		<Foot>
			<Closing>
				<xsl:apply-templates select="Name"/>
				<xsl:copy-of select="Title"/>
			</Closing>
		</Foot>
	</xsl:template>
	
	<xsl:template match="Name">
		<Name>
			<xsl:if test="Title">
				<xsl:attribute name="Title">
					<xsl:value-of select="Title"/>
				</xsl:attribute>
			</xsl:if>
			<FirstName><xsl:value-of select="FName"/></FirstName>
			<LastName><xsl:value-of select="LName"/></LastName>
		</Name>
	</xsl:template>
</xsl:stylesheet>