XML - 数据交换/数据模式管理(答案)
外观
- 在第 4 章中,您被要求创建一个模式文档来定义一家餐厅。在下一章中,您被要求创建一个模式来定义一家特许经营餐厅。在本练习中,您需要创建一个新的 XML 模式文档,并使用 redefine 机制通过追加必要的特许经营信息来扩展您的原始餐厅数据类型。修改您用于特许经营餐厅的 XML 实例文档,以使用新创建的 XML 模式文档进行验证。
- 创建一个包含下表中类型库的模式文档。现在创建一个新的模式文档,用于描述城市中的酒吧,使用 import 机制导入包含 USAddressType 的模式和命名空间,并使用 USAddressType 来定义酒吧的地址。创建一个包含至少两个酒吧的 XML 实例文档,用于验证任何美国城市的新的创建的模式文档。
<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>
表 7-11:练习 2 - 类型库 USAddress.xsd
- 检索您在第 2 章练习中创建的 XML 实例文档和 XML 模式文档。将 elementFormDefault 值更改为“qualified”,并在 XML 实例文档中进行必要的更改,使其能够正确验证。
- 为第 4 章中的 tourGuide 模式开发一个类型库。使用常识将相关的元素和数据类型分组。请记住,某些数据类型可以重用,或者可能需要泛化以允许它们重用(或重新定义)。
Danny Popov 的答案
问题 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>
|
问题 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>
|
问题 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>
|
问题 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>
|