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

From Wikibooks, open books for an open world
< XML - Managing Data Exchange‎ | The many-to-many relationship
Jump to: navigation, search

Many-to-Many Chapter => The Many-to-Many Relationship

Many-to-Many Exercises => Exercises

1) Movie Collection[edit]

Use the following data model:

Xml5moviedm.png

a. Using the IDREF method, create an XML schema to describe the illustrated data model. Include all of the attributes listed in the data model.
b. Create an XML document that includes at least two movies and two cast members per movie.
c. Create a simple XML style sheet to present the information in HTML format. No tables or CSS are necessary.

the Schema[edit]

<?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>

the Document[edit]

<?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>

the Stylesheet[edit]

<?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) Student/Class Directory[edit]

a. Every class at a school can have many students, and each student can be registered to more than one class at a time. Create an XML schema to capture this many-to-many relationship. Be sure to include the class name, capacity, building number, building name, and room number. Also include the student first/last name separately and his or her grade for the class.
b. Using the XML schema created in part a, create an XML document and an XML stylesheet. You can make up your own data, but be sure to use at least three students for each class.

the Schema[edit]

<?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>

the Document[edit]

<?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>

the Stylesheet[edit]

<?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>


Many-to-Many Chapter => The Many-to-Many Relationship

Many-to-Many Exercises => Exercises