XML - Managing Data Exchange/The one-to-one relationship/Answers

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Exercise 1[edit | edit source]

  1. Create an XML schema to describe the most popular restaurant in a city. Use either the all order indicator or the choice order indicator in your schema. Check that it is well-formed and valid.
  2. Using the schema, create an XML document and populate it with data for one city that has a most popular restaurant as well as two or more other restaurants. Check that it is well-formed and valid.
  3. Write an XML style sheet to display the most popular restaurants sorted by name if the restaurant is located in a city with population > 5,000.

XML schema[edit | edit source]

<?xml version="1.0" encoding="UTF-8"?>
<!--
Document  : city_restaurant.xsd
Created on : February 17, 2004, 5:55 PM
Author   : Shirley Loh
Description:
Purpose of XML Schema document follows.
-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified">
<!--
Directory
-->
<xsd:element name="directory">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="city" type="cityDetails" minOccurs="1"
maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!--
City
-->
<xsd:complexType name="cityDetails">
<xsd:sequence>
<xsd:element name="cityName" type="xsd:string"/>
<xsd:element name="adminUnit" type="xsd:string"/>
<xsd:element name="country" type="xsd:string"/>
<xsd:element name="population" type="xsd:integer"/>
<xsd:element name="area" type="xsd:integer"/>
<xsd:element name="elevation" type="xsd:integer"/>
<xsd:element name="longitude" type="xsd:decimal"/>
<xsd:element name="latitude" type="xsd:decimal"/>
<xsd:element name="description" type="xsd:string"/>
<xsd:element name="history" type="xsd:string"/> 
<xsd:element name="topRestaurant" type="restaurantDetails" 
minOccurs="1" maxOccurs="1"/>
<xsd:element name="restaurant" type="restaurantDetails" 
minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!--
Restaurant
-->
<xsd:complexType name="restaurantDetails">
<xsd:all>
<xsd:element name="restaurantName" type="xsd:string"/>
<xsd:element name="streetAddress" type="xsd:string"/>
<xsd:element name="phone" type="xsd:string"/>
<xsd:element name="fax" type="xsd:string" minOccurs="0"/>
<xsd:element name="websiteURL" type="xsd:anyURI" minOccurs="0"/>
<xsd:element name="capacity" type="xsd:integer" minOccurs="0"/>
<xsd:element name="cuisine" type="xsd:string"/>
<xsd:element name="operatingHour" type="xsd:string"/>
</xsd:all>
</xsd:complexType> 
</xsd:schema>

XML document[edit | edit source]

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl"
href="city_restaurant.xsl" media="screen"?>
<!--
Document  : city_restaurant.xml
Created on : February 17, 2004, 6:11 PM
Author   : Shirley Loh
Description:
Purpose of the document follows.
-->
<directory xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation='city_restaurant.xsd'>
<city>
<cityName>Belmopan</cityName>
<adminUnit>Cayo</adminUnit>
<country>Belize</country>
<population>11100</population>
<area>5</area>
<elevation>130</elevation>
<longitude>12.3</longitude>
<latitude>123.4</latitude>
<description>Belmopan is the capital of Belize</description>
<history>Belmopan was established following devastation of the
former capitol, Belize City, by Hurricane Hattie in 1965. 
High ground and open space influenced the choice and
ground-breaking began in 1966. By 1970 most government
offices and operations had already moved to the new
location.
</history>    
<topRestaurant>
<restaurantName>The Caladium Restaurant</restaurantName>
<streetAddress>Market Square</streetAddress>
<phone>501-08-22754</phone>   
<cuisine>Belizean and International</cuisine> 
<operatingHour>7:00 a.m. to 8:00 p.m. Mon thru Sat
</operatingHour>
</topRestaurant>
<restaurant>
<restaurantName>Bull Frog Restaurant</restaurantName>
<streetAddress>25 Half Moon Avenue</streetAddress>
<phone>501-822-3425</phone>
<capacity>90</capacity>
<cuisine>Belizean</cuisine>
<operatingHour>7:00 a.m. to 9:30 p.m. Mon thru Sun
</operatingHour>
</restaurant> 
</city>
<city>
<cityName>Kuala Lumpur</cityName>
<adminUnit>Selangor</adminUnit>
<country>Malaysia</country>
<population>1448600</population>
<area>243</area>
<elevation>111</elevation>
<longitude>101.71</longitude>
<latitude>3.16</latitude>
<description>Kuala Lampur is the capital of Malaysia and is the
largest city in the nation.</description>
<history>The city was founded in 1857 by Chinese tin miners and
superseded Klang. In 1880 the British government 
transferred their headquarters from Klang to Kuala
Lumpur, and in 1896 it became the capital of Malaysia. 
</history>
<topRestaurant>
<restaurantName>EEST</restaurantName>
<streetAddress>Level 1, 199 Jalan Bukit Bintang</streetAddress>
<phone>011-603-2731-8333</phone>
<fax>011-603-2731-8000</fax>
<websiteURL>http://www.alloexpat.com/westin_kuala
_lumpur_malaysia.htm</websiteURL>
<cuisine>Japanese and South East Asian</cuisine>
<operatingHour>11:00 a.m. to 11:30 p.m. Mon thru Sun
</operatingHour>
</topRestaurant>
<restaurant>
<restaurantName>1957 Bistro</restaurantName>
<streetAddress>22, Jalan 25/70A Desa Sri Hartamas</streetAddress>
<phone>011-603-430-1030</phone>
<cuisine>Fusion</cuisine>
<operatingHour>9:00 a.m. to 11:00 p.m. Mon thru Sun
</operatingHour>
</restaurant>
<restaurant>
<restaurantName>Herbal Soup House</restaurantName>
<streetAddress>19 Jalan Telawi Dua</streetAddress>
<phone>011-603-998-1232</phone>
<capacity>180</capacity>
<cuisine>Asian</cuisine>
<operatingHour>10:00 a.m. to 10:30 p.m. Mon thru Sun
</operatingHour>
</restaurant>
</city>
</directory>

XML stylesheet[edit | edit source]

<?xml version="1.0" encoding="UTF-8" ?>
<!--
Document  : city_restaurant.xsl
Created on : February 17, 2004, 6:38 PM
Author   : Shirley Loh
Description:
Purpose of transformation follows.
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Directory</TITLE>
<STYLE TYPE="text/css">
H2         {TEXT-ALIGN:CENTER;}    
.greenBackground  {BACKGROUND-COLOR:LIGHTGREEN;
TEXT-ALIGN:CENTER;}
.blueBackground   {BACKGROUND-COLOR:BLUE;
TEXT-ALIGN:CENTER;
FONT-WEIGHT:BOLD;
FONT-SIZE:14pt;}           
</STYLE>
</HEAD>
<BODY STYLE="BACKGROUND-COLOR:#ffff99">
<H2>Top Restaurant of the City</H2>
<xsl:apply-templates select="directory"/>
</BODY>
</HTML>
</xsl:template> 
<xsl:template match="directory">

{| BORDER="1" WIDTH="100%"
|- CLASS="blueBackground"

<TD><xsl:text>Restaurant</xsl:text>
| <xsl:text>City</xsl:text> || <xsl:text>Street Address</xsl:text>
| <xsl:text>Operating Hours</xsl:text>


<xsl:for-each select="city">
<xsl:sort select="topRestaurant/restaurantName"/>     
<xsl:if test="population &gt; 5000">  
|- CLASS="greenBackground"
| <xsl:value-of select="topRestaurant/restaurantName"/>
| <xsl:value-of select="cityName"/>
| <xsl:value-of select="topRestaurant/streetAddress"/>
| <xsl:value-of select="topRestaurant/operatingHour"/>


</xsl:if> 
</xsl:for-each>
|}
</xsl:template>
</xsl:stylesheet>


Exercise 2[edit | edit source]

  1. Create and XML schema to represent the most popular professors at a university. Include first name, last name, department, and years teaching.
  2. Using the XML schema, create and XML document and populate it with data about a university and its most popular professor. Use at least three universities and two professors at each university.
  3. Write and XML style sheet to display the most popular professors at each school sorted by the professor names.

XML schema[edit | edit source]

<nowiki>
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">
<xsd:element name="universityGuide">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="university" type="universityDetails" minOccurs="0" maxOccurs="unbounded" /> 
      </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:complexType name="universityDetails">
  <xsd:sequence>
    <xsd:element name="universityName" type="xsd:string"/>
    <xsd:element name="city" type="xsd:string"/>
    <xsd:element name="state" type="xsd:string"/>
    <xsd:element name="population" type="xsd:integer" default="0"/>
    <xsd:element name="area" type="xsd:integer"/>
    <xsd:element name="undergradNumber" type="xsd:integer"/>
    <xsd:element name="descriptionUniv" type="xsd:string"/> 
    <xsd:element name="topProf" type="professorDetails" minOccurs="0" maxOccurs="1" />
    <xsd:element name="professor" type="professorDetails" minOccurs="1" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:complexType name="professorDetails">
  <xsd:sequence>
    <xsd:element name="firstName" type="xsd:string"/>
    <xsd:element name="lastName" type="xsd:string"/>
    <xsd:element name="yearsTeaching" type="xsd:integer"/>     
    <xsd:element name="streetAddress" type="xsd:string" />      
    <xsd:element name="postalCode" type="xsd:string" />
    <xsd:element name="telephoneNumber" type="xsd:string"/>
    <xsd:element name="professorRating" type="xsd:integer" />
    </xsd:sequence>
    </xsd:complexType>
    </xsd:schema>
</nowiki>

XML document[edit | edit source]

<nowiki>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="OneToOneXMLStyleSheet.xsl" type="text/xsl" media="screen"?> 
<universityGuide xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
 xsi:noNamespaceSchemaLocation='OneToOneXMLSchema.xsd'>
 <university>
  <universityName>University of Georgia</universityName>
  <city>Athens</city>
  <state>GA</state>
  <population>32000</population>
  <area>3434</area>
  <undergradNumber>22000</undergradNumber>
  <descriptionUniv>Located in Athens, GA. This campus is home to over 32,000 students. Majors range from Biology to Management.</descriptionUniv>
  <topProf>
    <firstName>Rick</firstName>
    <lastName>Watson</lastName>
    <yearsTeaching>15</yearsTeaching>
    <streetAddress>12 Lumpkin Street</streetAddress>
    <postalCode>30606</postalCode>
    <telephoneNumber>706-338-5841</telephoneNumber>
    <professorRating>1</professorRating>   
  </topProf>
  <professor>
    <firstName>William</firstName>
    <lastName>Nichols</lastName>
    <yearsTeaching>11</yearsTeaching>
    <streetAddress>145 East Campus Road</streetAddress>
    <postalCode>30606</postalCode>
    <telephoneNumber>478-587-5817</telephoneNumber>
    <professorRating>15</professorRating>
  </professor>
</university>
<university>
  <universityName>University of Texas</universityName>
  <city>Austin</city>
  <state>TX</state>
  <population>50800</population>
  <area>4558</area>
  <undergradNumber>36000</undergradNumber>
  <descriptionUniv>This school is located in Austin, TX. This is one of the largest schools in the country.</descriptionUniv>
    <topProf>
    <firstName>Bob</firstName>
    <lastName>Pierce</lastName>
    <yearsTeaching>5</yearsTeaching>
    <streetAddress>5454 Bull Street</streetAddress>
    <postalCode>14704</postalCode>
    <telephoneNumber>897-854-7740</telephoneNumber>
    <professorRating>1</professorRating>   
  </topProf>
  <professor>
    <firstName>Iris</firstName>
    <lastName>Smith</lastName>
    <yearsTeaching>8</yearsTeaching>
    <streetAddress>54654 G Street</streetAddress>
    <postalCode>56548</postalCode>
    <telephoneNumber>548-517-6366</telephoneNumber>
    <professorRating>84</professorRating>
  </professor>
</university>
<university>
  <universityName>UCLA</universityName>
  <city>Los Angeles</city>
  <state>California</state>
  <population>15700</population>
  <area>8940</area>
  <undergradNumber>10000</undergradNumber>
  <descriptionUniv>This school is located in LA. This campus prides itself on very smart kids and LA's sunny weather.</descriptionUniv>
    <topProf>
    <firstName>Marie</firstName>
    <lastName>Potter</lastName>
    <yearsTeaching>32</yearsTeaching>
    <streetAddress>12 Beach Blvd</streetAddress>
    <postalCode>70481</postalCode>
    <telephoneNumber>879-454-5271</telephoneNumber>
    <professorRating>1</professorRating>
  </topProf>
   <professor>
    <firstName>Sue</firstName>
    <lastName>Harris</lastName>
    <yearsTeaching>32</yearsTeaching>
    <streetAddress>54 Main Street</streetAddress>
    <postalCode>56548</postalCode>
    <telephoneNumber>548-500-6366</telephoneNumber>
    <professorRating>18</professorRating>
  </professor>
</university>
<university>
  <universityName>Coastal Georgia Community College</universityName>
  <city>Brunswick</city>
  <state>GA</state>
  <population>3000</population>
  <area>100</area>
  <undergradNumber>2500</undergradNumber>
  <descriptionUniv>This is a community college located in Brunswick, GA. It serves the Golden Isles of Georgia.</descriptionUniv>
   <topProf>
    <firstName>Ribert</firstName>
    <lastName>Jones</lastName>
    <yearsTeaching>25</yearsTeaching>
    <streetAddress>5484 Altama Ave</streetAddress>
    <postalCode>31522</postalCode>
    <telephoneNumber>912-262-5484</telephoneNumber>
    <professorRating>1</professorRating>
   </topProf>
   <professor>
    <firstName>Brian</firstName>
    <lastName>Trip</lastName>
    <yearsTeaching>9</yearsTeaching>
    <streetAddress>5484 Altama Ave</streetAddress>
    <postalCode>31522</postalCode>
    <telephoneNumber>912-262-5485</telephoneNumber>
    <professorRating>154</professorRating>
  </professor>
</university>
</universityGuide>
</nowiki>

XML stylesheet[edit | edit source]

<nowiki>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>OneToOneXMLStylesheet.xsl - Universities and Professors</title>
      </head>
      <body>
      <h2>Universities and Professors</h2>
      <xsl:apply-templates select="universityGuide"/>
      </body>
     </html>
  </xsl:template>      
  <xsl:template match="universityGuide">
     
{| BORDER="1" WIDTH="80%" bgcolor="red"
<xsl:for-each select="university">
<xsl:sort select="lastName"/>
|-
| COLSPAN="5" bgcolor="yellow" align= "left" |
<BR/>
<b>
<xsl:value-of select="universityName"/>
</b>
<BR/>
<xsl:value-of select="city"/><xsl:text>, </xsl:text><xsl:value-of select="state"/>
<BR/>
<xsl:text>Population: </xsl:text>
<xsl:value-of select="population"/>
<BR/>
<xsl:text>Area: </xsl:text>
<xsl:value-of select="area"/>
<BR/>
<xsl:text>Undergrad Number: </xsl:text>
<xsl:value-of select="undergradNumber"/>
<BR/>
<BR/>
<b><xsl:text>University Description: </xsl:text></b>
<BR/>
<xsl:value-of select="descriptionUniv"/>
<BR/>


<xsl:for-each select="topProf">
|-
|
<b><xsl:text>Top Professor Information </xsl:text></b><BR/>
<xsl:value-of select="firstName"/><xsl:text> </xsl:text><xsl:value-of select="lastName"/>
<BR/>
<xsl:value-of select="streetAddress"/>
<BR/>
<b><xsl:text>Professor Rating</xsl:text></b>
<BR/>
<xsl:value-of select="professorRating"/>
<BR/>



</xsl:for-each> 
</xsl:for-each>
|}
</xsl:template>
</xsl:stylesheet>
</nowiki>