跳转到内容

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

来自维基教科书,开放的书籍,为一个开放的世界

多对多章节 => 多对多关系

多对多练习 => 练习

1) 电影收藏

[编辑 | 编辑源代码]

使用以下数据模型

a. 使用 IDREF 方法,创建 XML 模式以描述图示数据模型。包括数据模型中列出的所有属性。
b. 创建一个 XML 文档,其中包含至少两部电影和每部电影的两位演员。
c. 创建一个简单的 XML 样式表,以 HTML 格式呈现信息。不需要表格或 CSS。
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Document   : movies.xsd
    Created on : February 6, 2006
    Author     : Christina Serrano
    Description: XML schema describing structure of movie collection
-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"> 
<xsd:element name="movieCollection">     
   <xsd:complexType>
      <xsd:sequence>
         <xsd:element name="movie" type="movieDetails" minOccurs="1" maxOccurs="unbounded"/>
         <xsd:element name="castMember" type="castDetails" minOccurs="1" maxOccurs="unbounded"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:element>
   <xsd:complexType name="movieDetails">
      <xsd:sequence>
         <xsd:element name="movieTitle" type="xsd:string"/>
         <xsd:element name="movieSynopsis" type="xsd:string"/>
         <xsd:element name="role" type="roleDetails" minOccurs="1" maxOccurs="unbounded"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="roleDetails">
      <xsd:sequence>
         <xsd:element name="roleIDREF" type="xsd:IDREF"/>
         <xsd:element name="roleType" type="xsd:string"/>         
      </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="castDetails">
      <xsd:sequence>
         <xsd:element name="castMemberID" type="xsd:ID"/>
         <xsd:element name="castFirstName" type="xsd:string"/>
         <xsd:element name="castLastName" type="xsd:string"/>
         <xsd:element name="castSSN" type="ssnType" minOccurs="0"/>
         <xsd:element name="castGender" type="genderType"/>         
      </xsd:sequence>
   </xsd:complexType>
   <xsd:simpleType name="ssnType">
       <xsd:restriction base="xsd:string">
          <xsd:pattern value="\d{3}-\d{2}-\d{4}"/>
       </xsd:restriction>
   </xsd:simpleType>
   <xsd:simpleType name="genderType">
       <xsd:restriction base="xsd:string">
          <xsd:enumeration value="male"/>
          <xsd:enumeration value="female"/>
       </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="movies.xsl" type="text/xsl" media="screen"?>
<!--
    Document   : movies.xml
    Created on : February 6, 2006
    Author     : Christina Serrano
    Description: XML document describing movie collection
-->
<movieCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="movies.xsd">   
   <movie>
        <movieTitle>City of God</movieTitle>
        <movieSynopsis>
          Youth gangs took over the slums of Rio de Janiero during the 1960s and didn't relinquish their 
          stronghold until the mid-1980s. Only a sucker wouldn't have turned to crime and this is exactly 
          how naive teen Rocket views himself. Rocket shoots all of the action with his weapon of choice, 
          a camera.
        </movieSynopsis>
        <role>
            <roleIDREF>ar1</roleIDREF>
            <roleType>Lead Actor</roleType>
        </role>
        <role>
            <roleIDREF>lf1</roleIDREF>
            <roleType>Lead Actor</roleType>
        </role>
        <role>
            <roleIDREF>kf1</roleIDREF>
            <roleType>Lead Actress</roleType>
        </role>   
   </movie>
   <movie>
        <movieTitle>Hotel Rwanda</movieTitle>
        <movieSynopsis>
          Paul Rusesabagina, the manager of a European-owned hotel in Rwanda, created a secret refugee camp 
          for the Tutsi people during the brutal genocide committed against them by the Hutu people in 1994. His 
          efforts helped to save 1200 lives out of close to a million who were killed.
        </movieSynopsis>
        <role>
            <roleIDREF>dc1</roleIDREF>
            <roleType>Lead Actor</roleType>
        </role>
        <role>
            <roleIDREF>nn1</roleIDREF>
            <roleType>Supporting Actor</roleType>
        </role>
        <role>
            <roleIDREF>so1</roleIDREF>
            <roleType>Lead Actress</roleType>
        </role>             
   </movie> 
   <movie>
        <movieTitle>Dodgeball</movieTitle>
        <movieSynopsis>
          The story's protagonist, Peter LaFleur, is a charismatic underachiever and proprietor of a rundown gym called
          Average Joe's. Peter's humble gym catches the eye of White Goodman, the power-mullet-sporting, Fu-Manchu-d, 
          egomaniacal owner of Globo Gym, a gleaming monolith of fitness. White intends to take over Average Joe's, and 
          Peter's non-existent bookkeeping is making it all too easy for him. The only way Peter can save Average Joe's 
          is a showdown dodgeball competition against Globo Gym.
        </movieSynopsis>
        <role>
            <roleIDREF>bs1</roleIDREF>
            <roleType>Lead Actor</roleType>
        </role>
        <role>
            <roleIDREF>ct1</roleIDREF>
            <roleType>Lead Actress</roleType>
        </role>
        <role>
            <roleIDREF>vv1</roleIDREF>
            <roleType>Lead Actor</roleType>
        </role>               
   </movie>
   <movie>
        <movieTitle>Meet the Parents</movieTitle>
        <movieSynopsis>
          Greg Focker is head over heels in love with his girlfriend Pam, and is ready to pop the big question. 
          When his attempt to propose is thwarted by a phone call with the news that Pam's younger sister is 
          getting married, Greg realizes that the key to Pam's hand in marriage lies with her formidable father. 
        </movieSynopsis>
        <role>
            <roleIDREF>bs1</roleIDREF>
            <roleType>Lead Actor</roleType>
        </role>
        <role>
            <roleIDREF>tp1</roleIDREF>
            <roleType>Lead Actress</roleType>
        </role>
        <role>
            <roleIDREF>rd1</roleIDREF>
            <roleType>Lead Actor</roleType>
        </role>            
   </movie>
    <castMember>
        <castMemberID>ar1</castMemberID>
        <castFirstName>Alexandre</castFirstName>
        <castLastName>Rodrigues</castLastName>
        <castSSN>867-34-2949</castSSN>
        <castGender>male</castGender>       
    </castMember>
    <castMember>
        <castMemberID>lf1</castMemberID>
        <castFirstName>Leandro</castFirstName>
        <castLastName>Firmino da Hora</castLastName>
        <castSSN>839-59-8765</castSSN>
        <castGender>male</castGender>       
    </castMember>
    <castMember>
        <castMemberID>kf1</castMemberID>
        <castFirstName>Karina</castFirstName>
        <castLastName>Falcao</castLastName>
        <castSSN>987-34-2958</castSSN>
        <castGender>female</castGender>       
    </castMember>
    <castMember>
        <castMemberID>dc1</castMemberID>
        <castFirstName>Don</castFirstName>
        <castLastName>Cheadle</castLastName>
        <castSSN>849-39-4439</castSSN>
        <castGender>male</castGender>       
    </castMember> 
    <castMember>
        <castMemberID>nn1</castMemberID>
        <castFirstName>Nick</castFirstName>
        <castLastName>Nolte</castLastName>
        <castSSN>233-56-4309</castSSN>
        <castGender>male</castGender>       
    </castMember>  
    <castMember>
        <castMemberID>so1</castMemberID>
        <castFirstName>Sophie</castFirstName>
        <castLastName>Okonedo</castLastName>
        <castSSN>993-23-4958</castSSN>
        <castGender>female</castGender>       
    </castMember>
    <castMember>
        <castMemberID>rd1</castMemberID>
        <castFirstName>Robert</castFirstName>
        <castLastName>De Niro</castLastName>
        <castSSN>489-32-5984</castSSN>
        <castGender>male</castGender>       
    </castMember> 
    <castMember>
        <castMemberID>bs1</castMemberID>
        <castFirstName>Ben</castFirstName>
        <castLastName>Stiller</castLastName>
        <castSSN>590-59-2774</castSSN>
        <castGender>male</castGender>       
    </castMember>
    <castMember>
        <castMemberID>tp1</castMemberID>
        <castFirstName>Teri</castFirstName>
        <castLastName>Polo</castLastName>
        <castSSN>099-37-8765</castSSN>
        <castGender>female</castGender>       
    </castMember>  
    <castMember>
        <castMemberID>vv1</castMemberID>
        <castFirstName>Vince</castFirstName>
        <castLastName>Vaughn</castLastName>
        <castSSN>383-56-2095</castSSN>
        <castGender>male</castGender>       
    </castMember>      
    <castMember>
        <castMemberID>ct1</castMemberID>
        <castFirstName>Christine</castFirstName>
        <castLastName>Taylor</castLastName>
        <castSSN>309-49-4005</castSSN>
        <castGender>female</castGender>       
    </castMember>              
</movieCollection>

样式表

[编辑 | 编辑源代码]
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Document   : movies.xsl
    Created on : February 6, 2006
    Author     : Christina Serrano
    Description: XML stylesheet to format movie collection data
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="castList" match="castMember" use="castMemberID"/>
    <xsl:output method="html"/>
    <xsl:template match="/">
        <HTML>
            <HEAD>
                <TITLE>Movie Collection</TITLE>
            </HEAD>
            <BODY>
                <H2>Movie Collection</H2>
                <xsl:apply-templates select="movieCollection"/>
            </BODY>
        </HTML>
    </xsl:template>
    <xsl:template match="movieCollection">
        <xsl:for-each select="movie">
            <HR/>
            <BR/>
            <b><xsl:text>Movie Title: </xsl:text></b>
            <xsl:value-of select="movieTitle"/>
            <BR/>
            <BR/>
            <b><xsl:text>Movie Synopsis: </xsl:text></b>            
            <xsl:value-of select="movieSynopsis"/>
            <BR/>
            <BR/>
            <b><xsl:text>Cast: </xsl:text></b>
            <BR/>
            <xsl:for-each select="role">
                <xsl:value-of select="key('castList',roleIDREF)/castFirstName"/>
                 <xsl:text>   </xsl:text>
                <xsl:value-of select="key('castList',roleIDREF)/castLastName"/>               
                <xsl:text>,   </xsl:text>
                <xsl:value-of select="roleType"/>
                <BR/>
                <xsl:value-of select="key('castList',roleIDREF)/castGender"/>
                <xsl:text>,   </xsl:text>
                <xsl:value-of select="key('castList',roleIDREF)/castSSN"/>
                <BR/>
                <BR/>
            </xsl:for-each>                     
        </xsl:for-each>       
    </xsl:template>
</xsl:stylesheet>

2) 学生/班级目录

[编辑 | 编辑源代码]
a. 学校的每个班级都可以有许多学生,而每个学生可以同时注册多个班级。创建 XML 模式以捕获这种多对多关系。确保包括班级名称、容量、楼号、楼名和房间号。还包括学生的姓/名,以及他或她在班级的成绩。
b. 使用在步骤 a 中创建的 XML 模式,创建一个 XML 文档和一个 XML 样式表。您可以编造自己的数据,但请确保每个班级至少使用三名学生。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">
	<xsd:element name="classDirectory">
		<xsd:complexType>
			<xsd:sequence>
				<xsd:element name="class" type="classDetails" maxOccurs="unbounded"/>
				<xsd:element name="studentRef" type="studentID" maxOccurs="unbounded"/>
			</xsd:sequence>
		</xsd:complexType>
	</xsd:element>
	<xsd:complexType name="classDetails">
		<xsd:sequence>
			<xsd:element name="className" type="xsd:string"/>
			<xsd:element name="classLocation" type="locationDetails"/>
			<xsd:element name="classCapacity" type="xsd:integer"/>
			<xsd:element name="student" type="studentValue" maxOccurs="unbounded"/>
		</xsd:sequence>
	</xsd:complexType>
	<xsd:complexType name="locationDetails">
		<xsd:sequence>
			<xsd:element name="classRoomNumber" type="xsd:string"/>
			<xsd:element name="classBuildingName" type="xsd:string"/>
		</xsd:sequence>
	</xsd:complexType>
	<xsd:complexType name="studentValue">
		<xsd:sequence>
			<xsd:element name="studentIDREF" type="xsd:IDREF"/>
			<xsd:element name="studentGrade" type="xsd:string"/>
		</xsd:sequence>
	</xsd:complexType>
	<xsd:complexType name="studentID">
		<xsd:sequence>
			<xsd:element name="studentIDValue" type="xsd:ID"/>
			<xsd:element name="studentFirstName" type="xsd:string"/>
			<xsd:element name="studentLastName" type="xsd:string"/>
		</xsd:sequence>
	</xsd:complexType>
</xsd:schema>
<?xml version="1.0" encoding="UTF-8"?>
<classDirectory xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xsi:noNamespaceSchemaLocation='unidirectory6.xsd'>

    <class>
        <className>MATH 2200</className>
        <classLocation>
			<classRoomNumber>206B</classRoomNumber>
			<classBuildingName>Boyd</classBuildingName>
        </classLocation>
        <classCapacity>30</classCapacity>
        <student>
            <studentIDREF>a1</studentIDREF>
            <studentGrade>80</studentGrade>
        </student>
        <student>
            <studentIDREF>a2</studentIDREF>
            <studentGrade>98</studentGrade>
        </student>
        <student>
            <studentIDREF>a3</studentIDREF>
            <studentGrade>76</studentGrade>
        </student>
	</class>
	
	<class>
        <className>JOUR 3310</className>
        <classLocation>
			<classRoomNumber>102</classRoomNumber>
			<classBuildingName>MLC</classBuildingName>
        </classLocation>
        <classCapacity>150</classCapacity>
        <student>
            <studentIDREF>a4</studentIDREF>
            <studentGrade>80</studentGrade>
        </student>
        <student>
            <studentIDREF>a1</studentIDREF>
            <studentGrade>98</studentGrade>
        </student>
        <student>
            <studentIDREF>a5</studentIDREF>
            <studentGrade>76</studentGrade>
        </student>
	</class>
	
	<studentRef>
		<studentIDValue>a1</studentIDValue>
		<studentFirstName>John</studentFirstName>
		<studentLastName>Smith</studentLastName>
	</studentRef>
	
	<studentRef>
		<studentIDValue>a2</studentIDValue>
		<studentFirstName>Katie</studentFirstName>
		<studentLastName>Black</studentLastName>
	</studentRef>
	
	<studentRef>
		<studentIDValue>a3</studentIDValue>
		<studentFirstName>Charles</studentFirstName>
		<studentLastName>Fisher</studentLastName>
	</studentRef>
	
	<studentRef>
		<studentIDValue>a4</studentIDValue>
		<studentFirstName>Sarah</studentFirstName>
		<studentLastName>Dawson</studentLastName>
	</studentRef>
	
	<studentRef>
		<studentIDValue>a5</studentIDValue>
		<studentFirstName>Alex</studentFirstName>
		<studentLastName>Patton</studentLastName>
	</studentRef>

</classDirectory>

样式表

[编辑 | 编辑源代码]
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="sList" match="studentRef" use="studentIDValue"/>
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>Class Directory</title>
            </head>
            <body>
                <xsl:apply-templates select="classDirectory"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="classDirectory">
        <xsl:for-each select="class">
            <b><xsl:value-of select="className"/></b>
            <br/>
            <xsl:for-each select="student">
				<xsl:sort select="key('sList',studentIDREF)/studentLastName"/>
                <xsl:value-of select="key('sList',studentIDREF)/studentLastName"/>
                <xsl:text>, </xsl:text>
                <xsl:value-of select="key('sList',studentIDREF)/studentFirstName"/>
                <xsl:text>:   </xsl:text>
                <xsl:value-of select="studentGrade"/>
                <BR/>
            </xsl:for-each>
            <br/>
            <br/>
        </xsl:for-each>
        <br/>
    </xsl:template>
</xsl:stylesheet>


多对多章节 => 多对多关系

多对多练习 => 练习

华夏公益教科书