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

Chapter

edit

To return to the chapter, follow this link: One-to-many relationship

Exercises

edit

To view the exercises, follow this link: exercises

Answer - Exercise 1

edit

XML schema:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="museum_info">

  <xsd:complexType>
     <xsd:sequence>
        <xsd:element name="museum" type="museum_details" minOccurs="1" 
        maxOccurs="unbounded" />
     </xsd:sequence>
  </xsd:complexType>

</xsd:element> <xsd:complexType name="museum_details">

  <xsd:sequence>
     <xsd:element name="museumName" type="xsd:string"/>
     <xsd:element name="dateEstablished" type="xsd:int"/>
     <xsd:element name="address" type="xsd:string"/>
     <xsd:element name="link" type="xsd:string"/>
     <xsd:element name="mPicture"/>
     <xsd:element name="country">
     <xsd:simpleType>
        <xsd:restriction base="xsd:string">
           <xsd:enumeration value="Germany"/>
           <xsd:enumeration value="Turkey"/>
           <xsd:enumeration value="USA"/>
           <xsd:enumeration value="Canada"/>
           <xsd:enumeration value="Mexico"/>
           <xsd:enumeration value="France"/>
           <xsd:enumeration value="Holland"/>
           <xsd:enumeration value="Spain"/>
           <xsd:enumeration value="China"/>
           <xsd:enumeration value="Japan"/>
           <xsd:enumeration value="India"/>
        </xsd:restriction>
     </xsd:simpleType>
     </xsd:element>
     <xsd:element name="museumHours" type="hours" minOccurs="1"
        maxOccurs="unbounded" />
  </xsd:sequence>

</xsd:complexType> <xsd:complexType name="hours">

  <xsd:sequence>
     <xsd:element name="date" type="xsd:string"/>
     <xsd:element name="openingTime" type="xsd:string"/>
     <xsd:element name="location" type="xsd:string"/>
     <xsd:element name="eventType" type="xsd:string"/>
     <xsd:element name="eventDescription" type="xsd:string"/>
  </xsd:sequence> 

</xsd:complexType> </xsd:schema>


XML document:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

<museum_info xsi:noNamespaceSchemaLocation="museum.xsd">

  <museum>
     <museumName>Metropolitan Museum of Art</museumName>
     <dateEstablished>1870</dateEstablished>
     <address>1000 Fifth Avenue, New York, NY 10028</address>
     <link>http://metmuseum.org</link>
     <mPicture filename="themet.jpg"/>
     <country>USA</country>
     <museumHours>
        <date>02-17-2004</date>
        <openingTime>11:00 a.m.</openingTime>
        <location>Great Hall</location>
        <eventType>Gallery Talk</eventType>
        <eventDescription>
           Poets, Lovers, and Heroes in Italian Mythological Prints
        </eventDescription>
     </museumHours>
     <museumHours>
        <date>02-17-2004</date>
        <openingTime>11:00 a.m.</openingTime>
        <location>Uris Center for Education</location>
        <eventType>Family Program</eventType>
        <eventDescription>
           A gallery program for visitors ages five through twelve and
           accompanying adults to welcome them to the Museum's collection
           through discussions and sketching
        </eventDescription>
     </museumHours>
     <museumHours>
        <date>02-18-2004</date>
        <openingTime>6:00 p.m.</openingTime>
        <location>Grace Rainey Rogers Auditorium</location>
        <eventType>Lecture</eventType>
        <eventDescription>
           Curious Combinations in Siracusa and Palermo; a Baroque Stucco
           Oratory; and Churches in Modica and Ragusa Ibla
        </eventDescription>
     </museumHours>
     <museumHours>
        <date>02-19-2004</date>
        <openingTime>11:00 a.m.</openingTime>
        <location>Great Hall</location>
        <eventType>Gallery Talk</eventType>
        <eventDescription>
           Images of George Washington
        </eventDescription>
     </museumHours>
     <museumHours>
        <date>02-20-2004</date>
        <openingTime>2:00 p.m.</openingTime>
        <location>Vanderlyn's Panorama</location>
        <eventType>Lecture</eventType>
        <eventDescription>
           A Lacquer Penbox by Manohar: An Example of Late Safavid Style
           Painting in India
        </eventDescription>
     </museumHours>
     <museumHours>
        <date>02-20-2004</date>
        <openingTime>3:00 p.m.</openingTime>
        <location>Great Hall</location>
        <eventType>Gallery Talk</eventType>
        <eventDescription>
           Trade and Exchange in the Ancient Near East
        </eventDescription>
     </museumHours>
     <museumHours>
        <date>02-21-2004</date>
        <openingTime>11:00 a.m.</openingTime>
        <location> Grace Rainey Rogers Auditorium</location>
        <eventType>Film</eventType>
        <eventDescription>
           Like Water for Chocolate - this feature film weaves a romantic
           fable in which chocolate plays a pivotal role in the cuisine of a
           passionate young cook.
        </eventDescription>
     </museumHours>
  </museum>

</museum_info>



Answer - Exercise 2

edit


XML schema:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"> <!-- currencies --> <xsd:element name="currencies">

   <xsd:complexType>
       <xsd:sequence>
           <xsd:element name="currency" type="currencyDetails" minOccurs="1" maxOccurs="unbounded"/>
       </xsd:sequence>
   </xsd:complexType>

</xsd:element> <!-- countries with currencies--> <xsd:simpleType name="currencyCodeType">

   <xsd:restriction base="xsd:string">
       <xsd:pattern value="\w{3}"/>
   </xsd:restriction>

</xsd:simpleType> <xsd:complexType name="currencyDetails">

   <xsd:sequence>
       <xsd:element name="countryName" type="xsd:string"/>
       <xsd:element name="currencyCode" type="currencyCodeType"/>
       <xsd:element name="currencyName" type="xsd:string"/>
       <!-- the next line is not necessary for answering question 1 of the exercise--> 
       <xsd:element name="flagImage"/>
   </xsd:sequence>      

</xsd:complexType> </xsd:schema>


XML document:

1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="currency.xsl" type="text/xsl"?> <!-- Well, didn't quite took the correct image paths, because there is no internet connection available right now. I'm sure you can figure it out yourself! -->

<currencies xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'

 xsi:noNamespaceSchemaLocation='currency.xsd'>
   <currency>
       <countryName>Germany</countryName>
       <currencyCode>EUR</currencyCode>
       <currencyName>Euros</currencyName>
        <!-- the next line is not necessary for answering question 1 of the exercise--> 
      <flagImage filename="deutscheflagge.gif" value="German Flag" imageURL="http://www.deutschland.de"/>        
    </currency>
   <currency>
       <countryName>United Kingdom</countryName>
       <currencyCode>GBP</currencyCode>
       <currencyName>Pounds</currencyName>
        <!-- the next line is not necessary for answering question 1 of the exercise--> 
      <flagImage filename="flag.gif" value="Flag" imageURL="http://www.uk.co.uk"/>        
       </currency>
   <currency>
       <countryName>Australia</countryName>
       <currencyCode>AUD</currencyCode>
       <currencyName>Dollars</currencyName>
        <!-- the next line is not necessary for answering question 1 of the exercise--> 
      <flagImage filename="flag.gif" value="Flag" imageURL="http://www.australia.au"/>         
   </currency>
   <currency>
       <countryName>Tuvalu</countryName>
       <currencyCode>TVD</currencyCode>
       <currencyName>Tuvalu Dollars</currencyName>
        <!-- the next line is not necessary for answering question 1 of the exercise--> 
      <flagImage filename="flag.gif" value="Flag" imageURL="http://www.tavalu.tuv"/>         
   </currency>
   <currency>
       <countryName>United States of America</countryName>
       <currencyCode>USD</currencyCode>
       <currencyName>Dollars</currencyName>
        <!-- the next line is not necessary for answering question 1 of the exercise--> 
       <flagImage filename="flag.gif" value="Flag" imageURL="http://www.whitehouse.gov/"/>
       </currency>

</currencies>

Answer - Exercise 3

edit

XML schema:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  elementFormDefault="unqualified">
<!-- Spa Finder -->
 <xsd:element name="spaFinder">
  <xsd:complexType>
        <xsd:sequence>
         <xsd:element name="spa" type="spaDetails" minOccurs="1" 
              maxOccurs="unbounded"/> 
        </xsd:sequence>
  </xsd:complexType>
 </xsd:element>
<!-- Spa -->
 <xsd:complexType name="spaDetails">
  <xsd:sequence>
   <xsd:element name="spaName" type="xsd:string"/>
   <xsd:element name="spaOwner" type="xsd:string"/>
   <xsd:element name="spaPhone" type="xsd:string"/>
   <xsd:element name="spaCity" type="xsd:string"/>
   <xsd:element name="spaState" type="xsd:string"/>
   <xsd:element name="spaAddress" type="xsd:string"/>
   <xsd:element name="startedIn" type="xsd:date"/>
   <xsd:element name="spaType" type="xsd:string"/>
   <!--Activity is a complexType defined in the Spa to 
   indicate the one-to-many relationship between spa and activities.--> 
   <xsd:element name="activity" type="activityDetails" minOccurs="1" 
        maxOccurs="unbounded"/>
  </xsd:sequence> 
 </xsd:complexType>
<!-- Activity -->
 <xsd:complexType name="activityDetails">
  <xsd:sequence>
   <xsd:element name="activityName" type="xsd:string"/>
   <xsd:element name="description" type="xsd:string"/>
   <xsd:element name="price" type="xsd:decimal" />
 <!--Offering is a complexType defined in the Activities to 
   indicate the one-to-many relationship between activities and offerings.--> 
   <xsd:element name="offering" type="offeringDetails" minOccurs="1" 
        maxOccurs="unbounded"/>
  </xsd:sequence> 
 </xsd:complexType>
<!-- Offering -->
 <xsd:complexType name="offeringDetails">
  <xsd:sequence>
   <xsd:element name="days" type="xsd:string"/>
   <xsd:element name="time" type="xsd:string"/>
   <xsd:element name="practitioner" type="xsd:string" />
   <xsd:element name="floor" type="xsd:integer"/>
   <xsd:element name="room" type="xsd:string" />   
  </xsd:sequence>
 </xsd:complexType></xsd:schema>

Exercises

edit

To view the exercises, follow this link: exercises