跳转到内容

XML - 管理数据交换/一对一关系/答案

来自维基教科书,一个开放的世界中的开放书籍
  1. 创建一个 XML 模式来描述城市中最受欢迎的餐厅。在您的模式中使用所有顺序指示器或选择顺序指示器。检查它是否格式正确且有效。
  2. 使用该模式,创建一个 XML 文档并用一个城市的数据填充它,该城市有一家最受欢迎的餐厅以及两家或更多其他餐厅。检查它是否格式正确且有效。
  3. 编写一个 XML 样式表,以按名称对最受欢迎的餐厅进行排序,前提是该餐厅位于人口超过 5,000 人的城市。

XML 模式

[编辑 | 编辑源代码]
<?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 文档

[编辑 | 编辑源代码]
<?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 样式表

[编辑 | 编辑源代码]
<?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>


  1. 创建和 XML 模式以表示大学中最受欢迎的教授。包括姓名、姓氏、部门和任教年限。
  2. 使用 XML 模式,创建和 XML 文档并用关于一所大学及其最受欢迎的教授的数据填充它。使用至少三所大学,每所大学有两名教授。
  3. 编写一个 XML 样式表,以按教授姓名对每所学校中最受欢迎的教授进行排序。

XML 模式

[编辑 | 编辑源代码]
<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 文档

[编辑 | 编辑源代码]
<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 样式表

[编辑 | 编辑源代码]
<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>
华夏公益教科书