Chapter 18

Answers to exercises

1.
A business has a telephone directory that records the first and last name, telephone number and email address of everyone working in the firm. Departments are the main organizing unit of the firm so the telephone directory is typically displayed in department order and shows for each department the contact phone and fax number and email address.
1a.
Create a hierarchical data model for this problem.
1b.
Define a Data Schema
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="phonebook">
<xsd:complexType>
<xsd:sequence>
    <xsd:element maxOccurs="unbounded" minOccurs="1" name="department" type="deptType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="deptType">
    <xsd:sequence>
        <xsd:element name="deptname" type="xsd:string"/>
        <xsd:element name="deptphone" type="xsd:string"/>
        <xsd:element name="deptfax" type="xsd:string"/>
        <xsd:element name="deptemail" type="xsd:string"/>
        <xsd:element maxOccurs="unbounded" minOccurs="1" name="employee" type="empType"/>
    </xsd:sequence>
</xsd:complexType>
<xsd:complexType name="empType">
    <xsd:sequence>
        <xsd:element name="empid" type="xsd:integer"/>
        <xsd:element name="empfname" type="xsd:string"/>
        <xsd:element name="emplname" type="xsd:string"/>
        <xsd:element name="empphone" type="xsd:string"/>
        <xsd:element name="empemail" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>
			</xsd:schema>
1c.
Create an XML file containing some directory data
<?xml version="1.0" encoding="UTF-8"?>
<phonebook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:/Volumes/VST/XML/phonebook.xsd">
    <department>
        <deptname>Sales</deptname>
        <deptphone>706.542.3700</deptphone>
        <deptfax>706.542.3743</deptfax>
        <deptemail>sales@yourbiz.com</deptemail>
        <employee>
            <empid>18890</empid>
            <empfname>James</empfname>
            <emplname>Smith</emplname>
            <empphone>706.542.3702</empphone>
            <empemail>jsmith@yourbix.com</empemail>
        </employee>
        <employee>
            <empid>18990</empid>
            <empfname>Joanna</empfname>
            <emplname>Smart</emplname>
            <empphone>706.542.3703</empphone>
            <empemail>jsmart@yourbix.com</empemail>
        </employee>
        <employee>
            <empid>18732</empid>
            <empfname>Mary</empfname>
            <emplname>Hunt</emplname>
            <empphone>706.542.3704</empphone>
            <empemail>mhunt@yourbix.com</empemail>
        </employee>
    </department>
</phonebook>
1d.
Create an XSL file containing a stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
	<title>Company phonebook </title> 
<body>
	<table border="2">
		<tr bgcolor="khaki">
			<th align="left">
				Department Name 
			</th>
			<th align="left">
				Department Phone 
			</th>
			<th align="left">
				Department Fax 
			</th>
			<th align="left">
				Department email 
			</th>
		</tr>
		<tr>
			<td align="left">
				<xsl:value-of select="//deptname" />
			</td>
			<td align="left">
				<xsl:value-of select="//deptphone" />
			</td>
			<td align="left">
				<xsl:value-of select="//deptfax" />
			</td>
			<td align="left">
				<xsl:value-of select="//deptemail" />
			</td>
		</tr>
	</table>
	<p />
	<table border="2">
		<tr bgcolor="beige">
			<th colspan="3">
				employees 
			</th>
		</tr>
		<tr bgcolor="beige">
			<th align="left" rowspan="5">
				employee Id 
			</th>
			<th align="left" rowspan="5">
				Firstname 
			</th>
			<th align="left" rowspan="5">
				Lastname 
			</th>
			<th align="left" rowspan="5">
				Phone 
			</th>
			<th align="left" rowspan="5">
				Email 
			</th>
		</tr>
		<xsl:for-each select=" phonebook /department/* ">
			<tr>
				<td align="left">
					<xsl:value-of select=".//empid" />
				</td>
				<td>
					<xsl:value-of select=".// empfname" />
				</td>
				<td align="center">
					<xsl:value-of select=".//emplname" />
				</td>
				<td align="left">
					<xsl:value-of select=".//empphone" />
				</td>
				<td align="left">
					<xsl:value-of select=".//empemail" />
				</td>
			</tr>
		</xsl:for-each>
	</table>
	</body>
	</html>
</xsl:template>
</xsl:stylesheet>
4.
Take a page in the dictionary and do the following:
4a.
Create a hierarchical data model for this problem
4b.
Create a data schema
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="dictionary">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element maxOccurs="unbounded" minOccurs="1" name="entry" type="entryType"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:complexType name="entryType">
        <xsd:sequence>
            <xsd:element name="word" type="xsd:string"/>
            <xsd:element name="pronounciation" type="xsd:string"/>
            <xsd:element maxOccurs="unbounded" minOccurs="1" name="def" type="defType"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="defType">
        <xsd:sequence>
            <xsd:element name="defid" type="xsd:integer"/>
            <xsd:element name="definition" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>
4c.
Create an XML file containing some dictionary data
<?xml version="1.0" encoding="UTF-8"?>
<dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="dictionary.xsd">
    <entry>
        <word>anteater</word>
        <pronounciation>Ant'-eat`er</pronounciation>
        <def>
            <defid>1</defid>
            <definition>mammal that resides in warm climates and survives primarily on eating ants and termites</definition>
        </def>
    </entry>
    <entry>
        <word>anteater</word>
        <pronounciation>An*te'ri*or</pronounciation>
        <def>
            <defid>1</defid>
            <definition>Before in time; antecedent</definition>
        </def>
        <def>
            <defid>2</defid>
            <definition>Before, or toward the front, in place; as, the anterior part of the mouth; -- opposed to posterior</definition>
        </def>
    </entry>
</dictionary>
4d.
Create an XSL file containing a stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8" indent="yes" method="html" version="1.0"/>
    <xsl:template match="/">
        <html>
            <head>
                <title> Partial Dictionary </title>
            </head>
            <body>
                <h1>Dictionary </h1>
                <xsl:apply-templates select="dictionary"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="dictionary">
        <xsl:for-each select="entry">
            <br/>
            <font color="maroon">
                <xsl:value-of select="word"/>:  <xsl:value-of select="pronounciation"/>
            </font>
            <br/>
            <table>
                <xsl:for-each select="def">
                    <tr>
                        <td align="left">
                            <xsl:value-of select="defid"/>
                        </td>
                        <td>
                            <xsl:value-of select="definition"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
            <br/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

This page is part of the promotional and support material for Data Management (fifth edition) by Richard T. Watson
For questions and comments please contact the author