XML - Managing Data Exchange/Data Schemas (Answers)
Exercises
edit- In chapter 4, you were asked to create a schema document to define a restaurant. In the next chapter you were asked to create a schema defining a franchise restaurant. In this exercise, you need to create a new XML Schema document and use the redefine mechanism to extend your original restaurant data type by appending the necessary franchise information. Amend the XML instance document you used for your franchise restaurant to use the newly created XML schema document for validation.
-
Create a schema document that contains the type library in the table below. Now create a new schema document that will be used to describe bars in a city, use the import mechanism to import the schema and namespace that contains the USAddressType, and use the USAddressType to define the address of the bar. Create an XML instance document with at least two bars for any US city to validate against the newly created schema document.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"<br /> targetNamespace="http://www.arches.uga.edu/~nocturne/NS/USAddress"<br /> xmlns="http://www.arches.uga.edu/~nocturne/NS/USAddress"<br /> elementFormDefault="unqualified"><br /> <xsd:complexType name="USAddressType"><br /> <xsd:sequence> <br /> <xsd:element name="Street" type="xsd:string"/><br /> <xsd:element name="City" type="xsd:string"/><br /> <xsd:element name="State" type="xsd:string"/> <br /> <xsd:element name="ZipCode" type="xsd:string"/> <br /> </xsd:sequence> <br /></xsd:complexType> <br /></xsd:schema>
Table 7-11: Exercise 2 – Type Library USAddress.xsd
- Retrieve the XML instance document and XML schema document you created in the exercises for chapter 2. Change the elementFormDefault value to ‘qualified’ and make the necessary changes to the XML instance document so that it will validate correctly.
- Develop a type library for the tourGuide schema in chapter 4. Use common sense to group related elements and data types. Keep in mind that some data types may be reusable or may need to be generalized to allow them to be reused (or redefined).
Answers by Danny Popov
Question 1
chapter4.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!--******************************************MAIN******************************************-->
<xs:element name="tourGuide">
<xs:complexType>
<xs:sequence>
<xs:element name="country" type="countryDetails" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!--**************************************END MAIN****************************************-->
<!--*******************************Country**************************************************-->
<xs:complexType name="countryDetails">
<xs:sequence>
<xs:element name="countryName" type="xs:string"/>
<xs:element name="population" type="xs:integer" default="0" minOccurs="0"/>
<xs:element name="continent" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Asia"/>
<xs:enumeration value="Africa"/>
<xs:enumeration value="Australasia"/>
<xs:enumeration value="Europe"/>
<xs:enumeration value="North America"/>
<xs:enumeration value="South America"/>
<xs:enumeration value="Antarctica"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="city" type="cityDetails" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!--*******************************************END COUNTRY**********************************-->
<!--********************************************CITY************************************************-->
<xs:complexType name="cityDetails">
<xs:sequence>
<xs:element name="cityName" type="xs:string"/>
<xs:element name="population" type="xs:integer" default="0"/>
<xs:element name="restaurant" type="restaurantType" maxOccurs="unbounded"/>
<xs:element name="topRestaurant" type="restaurantType"/>
</xs:sequence>
</xs:complexType>
<!--*********************************************END CITY***************************************************-->
<!--********************************************RESTAURANT TYPE*****************************************-->
<xs:complexType name="restaurantType">
<xs:sequence>
<xs:element name="restaurantName" type="xs:string"/>
<xs:element name="restaurantStreetAddress" type="xs:string"/>
<xs:element name="restaurantZipcode" type="xs:string" minOccurs="0"/>
<xs:element name="phoneNumber" type="xs:string"/>
<xs:element name="foodType" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<!--********************************************END RESTAURANT TYPE*****************************************-->
</xs:schema>
|
Chap4Redefined.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:redefine schemaLocation="chapter4.xsd">
<xsd:complexType name="restaurantType">
<xsd:complexContent>
<xsd:extension base="restaurantType">
<xsd:sequence>
<xsd:element name="FranchiseNumber" type="xsd:integer" minOccurs="0"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:redefine>
</xsd:schema>
|
Chap4Redefined.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Chapter4.xsl"?>
<tourGuide xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Chap4Redefined.xsd">
<country>
<countryName>Canada</countryName>
<population>500000</population>
<continent>North America</continent>
<city>
<cityName>Toronto</cityName>
<population>30000</population>
<restaurant>
<restaurantName>Joe's Canadian Food</restaurantName>
<restaurantStreetAddress>String</restaurantStreetAddress>
<restaurantZipcode>String</restaurantZipcode>
<phoneNumber>String</phoneNumber>
<foodType>Canadian</foodType>
<FranchiseNumber>1</FranchiseNumber>
</restaurant>
<restaurant>
<restaurantName>Canadian Bacon</restaurantName>
<restaurantStreetAddress>String</restaurantStreetAddress>
<restaurantZipcode>95786</restaurantZipcode>
<phoneNumber>666-555-4444</phoneNumber>
<foodType>Breakfast</foodType>
</restaurant>
<topRestaurant>
<restaurantName>McDonalds</restaurantName>
<restaurantStreetAddress>555 1st Street</restaurantStreetAddress>
<restaurantZipcode>95786</restaurantZipcode>
<phoneNumber>800-555-5555</phoneNumber>
<foodType>Fast Food</foodType>
<FranchiseNumber>777432223</FranchiseNumber>
</topRestaurant>
</city>
</country>
<country>
<countryName>United States</countryName>
<population>800000</population>
<continent>North America</continent>
<city>
<cityName>Athens</cityName>
<population>30000</population>
<restaurant>
<restaurantName>Compadres</restaurantName>
<restaurantStreetAddress>333 Clayton Street</restaurantStreetAddress>
<restaurantZipcode>30606</restaurantZipcode>
<phoneNumber>706-222-2222</phoneNumber>
<foodType>Mexican</foodType>
<FranchiseNumber>4</FranchiseNumber>
</restaurant>
<restaurant>
<restaurantName>Longhorn Steakhouse</restaurantName>
<restaurantStreetAddress>333 Baxter St.</restaurantStreetAddress>
<restaurantZipcode>30606</restaurantZipcode>
<phoneNumber>706-666-6666</phoneNumber>
<foodType>American</foodType>
<FranchiseNumber>232</FranchiseNumber>
</restaurant>
<topRestaurant>
<restaurantName>Johnny Carinos</restaurantName>
<restaurantStreetAddress>224 Timothy Rd.</restaurantStreetAddress>
<restaurantZipcode>30607</restaurantZipcode>
<phoneNumber>706-555-5555</phoneNumber>
<foodType>Italian</foodType>
</topRestaurant>
</city>
<city>
<cityName>Athens</cityName>
<population>4000</population>
<restaurant>
<restaurantName>Burger King</restaurantName>
<restaurantStreetAddress>212 Some Road</restaurantStreetAddress>
<restaurantZipcode>30707</restaurantZipcode>
<phoneNumber>706-999-9999</phoneNumber>
<foodType>Fast Food</foodType>
<FranchiseNumber>553455345</FranchiseNumber>
</restaurant>
<restaurant>
<restaurantName>Wendy's</restaurantName>
<restaurantStreetAddress>312 Some road</restaurantStreetAddress>
<restaurantZipcode>30707</restaurantZipcode>
<phoneNumber>706-888-8888</phoneNumber>
<foodType>Fast Food</foodType>
<FranchiseNumber>343234234</FranchiseNumber>
</restaurant>
<topRestaurant>
<restaurantName>Silver Screen Grill</restaurantName>
<restaurantStreetAddress>420 Lexington Rd.</restaurantStreetAddress>
<restaurantZipcode>30707</restaurantZipcode>
<phoneNumber>706-111-1111</phoneNumber>
<foodType>American</foodType>
</topRestaurant>
</city>
</country>
</tourGuide>
|
Question 2
USAddress.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.arches.uga.edu/~nocturne/NS/USAddress" xmlns="http://www.arches.uga.edu/~nocturne/NS/USAddress" elementFormDefault="unqualified">
<xsd:complexType name="USAddressType">
<xsd:sequence>
<xsd:element name="Street" type="xsd:string"/>
<xsd:element name="City" type="xsd:string"/>
<xsd:element name="State" type="xsd:string"/>
<xsd:element name="ZipCode" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
|
Bar.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!--Danny Popov -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"
targetNamespace = "http://www.arches.uga.edu/~nocturne/NS/Bar"
xmlns = "http://www.arches.uga.edu/~nocturne/NS/Bar"
xmlns:addr = "http://www.arches.uga.edu/~nocturne/NS/USAddress">
<xsd:import namespace="http://www.arches.uga.edu/~nocturne/NS/USAddress"
schemaLocation="USAddress.xsd"/>
<xsd:element name="BarInfo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Bar" type="barType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="barType">
<xsd:sequence>
<xsd:element name="BarName" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="BarAddress" type="addr:USAddressType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
|
Bar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--Danny Popov -->
<bar:BarInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.arches.uga.edu/~nocturne/NS/Bar Bar.xsd"
xmlns:bar="http://www.arches.uga.edu/~nocturne/NS/Bar">
<Bar>
<BarName>The Globe</BarName>
<BarAddress>
<Street>333 Clayton St.</Street>
<City>Athens</City>
<State>GA</State>
<ZipCode>30605</ZipCode>
</BarAddress>
</Bar>
<Bar>
<BarName>40 Watt</BarName>
<BarAddress>
<Street>222 Clayton St.</Street>
<City>Athens</City>
<State>GA</State>
<ZipCode>30605</ZipCode>
</BarAddress>
</Bar>
</bar:BarInfo>
|
Question 3
Museum.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.arches.uga.edu/~dpopov/Museum" xmlns="http://www.arches.uga.edu/~dpopov/Museum" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="MuseumDirectory">
<xs:complexType>
<xs:sequence>
<xs:element name="MuseumInfo" type="MuseumInfo" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="MuseumInfo">
<xs:sequence>
<xs:element name="MuseumName" type="xs:string"/>
<xs:element name="dateEstablished" type="xs:date"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="url" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
|
Museum.xml
<?xml version="1.0" encoding="UTF-8"?>
<Museum:MuseumDirectory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Museum.xsd" xmlns:Museum="http://www.arches.uga.edu/~dpopov/Museum">
<Museum:MuseumInfo>
<Museum:MuseumName>Danny's Museum</Museum:MuseumName>
<Museum:dateEstablished>2000-01-01</Museum:dateEstablished>
<Museum:address>104 My Parkway</Museum:address>
<Museum:url>http://www.dannymuseum.com</Museum:url>
</Museum:MuseumInfo>
<Museum:MuseumInfo>
<Museum:MuseumName>Antique UFOs</Museum:MuseumName>
<Museum:dateEstablished>2999-01-01</Museum:dateEstablished>
<Museum:address>Third Cloud Station</Museum:address>
<Museum:url>http://www.antiqueufos.com</Museum:url>
</Museum:MuseumInfo>
</Museum:MuseumDirectory>
|
Question 4
TypeLibrary.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!--******************************************MAIN******************************************-->
<xs:element name="tourGuide">
<xs:complexType>
<xs:sequence>
<xs:element name="country" type="countryDetails" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!--**************************************END MAIN****************************************-->
<!--*******************************Country**************************************************-->
<xs:complexType name="countryDetails">
<xs:sequence>
<xs:element name="countryName" type="xs:string"/>
<xs:element name="population" type="xs:integer" default="0" minOccurs="0"/>
<xs:element name="continent" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Asia"/>
<xs:enumeration value="Africa"/>
<xs:enumeration value="Australasia"/>
<xs:enumeration value="Europe"/>
<xs:enumeration value="North America"/>
<xs:enumeration value="South America"/>
<xs:enumeration value="Antarctica"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="city" type="cityDetails" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!--*******************************************END COUNTRY**********************************-->
<!--********************************************CITY************************************************-->
<xs:complexType name="cityDetails">
<xs:sequence>
<xs:element name="cityName" type="xs:string"/>
<xs:element name="population" type="xs:integer" default="0"/>
<xs:element name="restaurant" type="restaurantType" maxOccurs="unbounded"/>
<xs:element name="topRestaurant" type="restaurantType"/>
</xs:sequence>
</xs:complexType>
<!--*********************************************END CITY***************************************************-->
<!--********************************************RESTAURANT TYPE*****************************************-->
<xs:complexType name="restaurantType">
<xs:sequence>
<xs:element name="restaurantName" type="xs:string"/>
<xs:element name="restaurantStreetAddress" type="xs:string"/>
<xs:element name="restaurantZipcode" type="xs:string" minOccurs="0"/>
<xs:element name="phoneNumber" type="xs:string"/>
<xs:element name="foodType" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<!--********************************************END RESTAURANT TYPE*****************************************-->
</xs:schema>
|