< XML - Managing Data Exchange < Recursive relationships

Exercises 2a

  • Create a schema describing one team in a relay race. The information needs to describe the runners in the race as well as from whom the runner received the baton. Create an XML file containing sample data for this schema and populate it with at least four runners. Make sure the XML document is well formed and valid.

Answer:

<?xml version="1.0" encoding="UTF-8"?>

<!--

   Document   : appetizerXML.xml
   Created on : March 28, 2004, 9:46 PM
   Author     : Jess Russell
   Description:
       Purpose of the document follows.

-->

<race xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="relayXSD.xsd">

   <runner primaryKey="1">
       <firstName>Jesse</firstName>
       <lastName>Owens</lastName>
   </runner>
   <runner primaryKey="2">
       <firstName>Bruce</firstName>
       <lastName>Jenner</lastName>
       <predecessor foreignKey="1"/>
   </runner>
   <runner primaryKey="3">
       <firstName>Clarke</firstName>
       <lastName>Kent</lastName>
       <predecessor foreignKey="2"/>
   </runner>
   <runner primaryKey="4">
       <firstName>Steve</firstName>
       <lastName>Austin</lastName>
       <predecessor foreignKey="3"/>
   </runner>
   <runner primaryKey="5">
       <firstName>Jamie</firstName>
       <lastName>Summers</lastName>
       <predecessor foreignKey="4"/>
   </runner>
   <runner primaryKey="6">
       <firstName>Road</firstName>
       <lastName>Runner</lastName>
       <predecessor foreignKey="5"/>
   </runner>

</race>

File relayXML.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--

   Document   : relayXSD.xml
   Created on : March 28, 2004, 9:46 PM
   Author     : Jess Russell
   Description:
       Purpose of the document follows.

-->

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified">

   <xsd:element name="race">
       <xsd:complexType>
                   <xsd:sequence>
                       <xsd:element name="runner" type="runnerType" maxOccurs="10">    
                       </xsd:element>
                   </xsd:sequence>
       </xsd:complexType>
               <xsd:keyref name="predecessorKey" refer="runKey">
                       <xsd:selector xpath="runner/predecessor"/>
                       <xsd:field xpath="@foreignKey"/>
               </xsd:keyref>
       <xsd:unique name="runIdChecker">
           <xsd:selector xpath="runner"/>
           <xsd:field xpath="@primaryKey"/>
       </xsd:unique>
       <xsd:unique name="oneToOneChecker">
           <xsd:selector xpath="runner/predecessor"/>
           <xsd:field xpath="@foreignKey"/>
       </xsd:unique>
       <xsd:key name="runKey">
           <xsd:selector xpath="runner"/>
           <xsd:field xpath="@primaryKey"/>
       </xsd:key>
   </xsd:element>
   
   <xsd:complexType name="runnerType">
       <xsd:sequence>
           <xsd:element name="firstName" type="xsd:string"/>
           <xsd:element name="lastName" type="xsd:string"/>
           <xsd:element name="predecessor" type="predecessorType" minOccurs="0" maxOccurs="1"/>
       </xsd:sequence>
       <xsd:attribute name="primaryKey" type="xsd:long" use="required"/>
   </xsd:complexType>
   
   <xsd:complexType name="predecessorType">
       <xsd:attribute name="foreignKey" type="xsd:long" use="required"/>
   </xsd:complexType>

</xsd:schema>

File relayXSD.xsd

<?xml version="1.0" encoding="UTF-8"?>

&lt!--

   Document   : appetizerXML.xml
   Created on : March 28, 2004, 9:46 PM
   Author     : Jess Russell
   Description:
       Purpose of the document follows.

-->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:key name="predecessor" match="runner" use="@primaryKey"/> <xsl:output method="html"/> <xsl:template match="/">

       <html>
       <head>
       <style>
       BODY {background-color:darkblue}
       </style>
       </head>
       <body>
           <table style="background-color:gray; color:darkblue;margin-left:auto; margin-right:auto">
               <tr style="background-color:lightblue;font-weight:bold">
                       <td>Runner First Name</td>
                       <td>Runner Last Name</td>
                       <td>Predecessor First Name</td>
                       <td>Predecessor Last Name</td>
               </tr>
               <xsl:for-each select="//runner">
                   <tr style="background-color:salmon">
                       <td>
                           <xsl:value-of select="firstName"/>
                       </td>
                       <td>
                           <xsl:value-of select="lastName"/>
                       </td>
                       <xsl:choose>
                            
                           <xsl:when test="key('predecessor',predecessor/@foreignKey)">
                                
                               <td>
                                   <xsl:value-of select="key('predecessor',predecessor/@foreignKey)/firstName"/>
                               </td>
                               <td>
                                   <xsl:value-of select="key('predecessor',predecessor/@foreignKey)/lastName"/>
                               </td>
                           </xsl:when>
                           <xsl:otherwise>
                                
                               <td colspan="2" style="text-align:center">This Runner started the race</td>
                           </xsl:otherwise>
                       </xsl:choose>
                   </tr>
               </xsl:for-each>
           </table>            </body>
           </html>

</xsl:template>

</xsl:stylesheet>

File relayXSL.xsl

  • Each state has many life insurance sales people, and each region (North, South, East, West) of the state has one regional head salesperson. A salesperson can only sell in one region and can only have one head salesperson he/she is working under. Create a schema to describe this relationship. Create an XML document and populate it with data for two states. Make sure the XML document is well formed and valid. Each state should have data for at least two regions, and each region should have at least three salesman, including the region head. Each salesperson should be uniquely identified by a number. Each state shoud be uniquely identified by its two letter abbreviation. Create an XSL stylesheet to display this data, with a separate table for each state. If the salesperson is a regional head, it should say so. Create an entity named doublespace in the stylesheet that is the equivalent to two carriage returns. Use the new entity to create two blank lines between the two tables.

Answer:

<?xml version="1.0" encoding="UTF-8"?>

<?xml version="1.0" encoding="UTF-8"?>

<!--

   Document   : insuranceXML.xml
   Created on : March 21, 2004, 11:26 PM
   Author     : russell
   Description:
       Purpose of the document follows.

-->

<insuranceInfo xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'

 xsi:noNamespaceSchemaLocation='file:/Users/russell/Courses/MIST7700/XML/ch6-assn/insuranceXSD.xsd'>

<state stateAbbr = "GA"> <stateName>Georgia</stateName>

   <region regionId = "1">
       <regionName>East</regionName>
       <salesPerson salesPersonId="1">
           <firstName>Joe</firstName>
           <lastName>Schmo</lastName>
       </salesPerson>
       <salesPerson salesPersonId="2">
           <firstName>Jim</firstName>
           <lastName>Beam</lastName>
           <regionHead fk_salesPersonId = "1"/>
       </salesPerson>
       <salesPerson salesPersonId="3">
           <firstName>Johnny</firstName>
           <lastName>Walker</lastName>
           <regionHead fk_salesPersonId = "1"/>
       </salesPerson>
   </region>
   <region regionId="2">
       <regionName>West</regionName>
       <salesPerson salesPersonId="4">
           <firstName>Ian</firstName>
           <lastName>Fleming</lastName>
       </salesPerson>
       <salesPerson salesPersonId="5">
           <firstName>Tom</firstName>
           <lastName>Clancy</lastName>
           <regionHead fk_salesPersonId = "4"/>
       </salesPerson>
           <salesPerson salesPersonId="6">
           <firstName>Conan</firstName>
           <lastName>Doyle</lastName>
           <regionHead fk_salesPersonId = "4"/>
       </salesPerson>
   </region>
   </state>
   <state stateAbbr = "NJ">
   <stateName>New Jersey</stateName>
   <region regionId = "3">
   <regionName>East</regionName>
       <salesPerson salesPersonId="7">
           <firstName>Tony</firstName>
           <lastName>Stark</lastName>
       </salesPerson>
       <salesPerson salesPersonId="8">
           <firstName>Peter</firstName>
           <lastName>Parker</lastName>
           <regionHead fk_salesPersonId = "7"/>
       </salesPerson>
       <salesPerson salesPersonId="9">
           <firstName>Johnny</firstName>
           <lastName>Storm</lastName>
           <regionHead fk_salesPersonId = "7"/>
       </salesPerson>
   </region>
   <region regionId="4">
       <regionName>West</regionName>
       <salesPerson salesPersonId="10">
           <firstName>Sue</firstName>
           <lastName>Storm</lastName>
       </salesPerson>
       <salesPerson salesPersonId="11">
           <firstName>Bruce</firstName>
           <lastName>Banner</lastName>
           <regionHead fk_salesPersonId = "10"/>
       </salesPerson>
       <salesPerson salesPersonId="12">
           <firstName>Conan</firstName>
           <lastName>Thebarbarian</lastName>
           <regionHead fk_salesPersonId = "10"/>
       </salesPerson>
   </region>

</state> </insuranceInfo>

File insuranceXML.xml

<?xml version="1.0" encoding="UTF-8"?>

&lt!--

   Document   : appetizerXML.xml
   Created on : March 28, 2004, 9:46 PM
   Author     : Jess Russell
   Description:
       Purpose of the document follows.

-->

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

   <xsd:element name="insuranceInfo">
   <xsd:complexType>
   <xsd:sequence>    
      <xsd:element name="state" maxOccurs="unbounded">
           <xsd:complexType>
               <xsd:sequence>
                   <xsd:element name="stateName" type="xsd:string"/> 
                   <xsd:element name="region" type="regionInfo" maxOccurs="4"/>
               </xsd:sequence>
               <xsd:attribute name="stateAbbr" type="xsd:string" use="required"/>
           </xsd:complexType>
           <xsd:unique name="regionChecker">
               <xsd:selector xpath="region"/>
               <xsd:field xpath="@regionId"/>
           </xsd:unique>
           <xsd:unique name="salesPersonChecker">
               <xsd:selector xpath="region/salesPerson"/>
               <xsd:field xpath="@salesPersonId"/>
           </xsd:unique>
           <xsd:key name="salesPersonKey">
               <xsd:selector xpath="salesPerson"/>
               <xsd:field xpath="@salesPersonId"/>
           </xsd:key>
           <xsd:keyref name="salesPersonForeignKey" refer="salesPersonKey">
               <xsd:selector xpath="state"/>
               <xsd:field xpath="@fk_salesPersonId"/>
           </xsd:keyref>
       </xsd:element>
       </xsd:sequence>
       </xsd:complexType>
       <xsd:unique name="stateChecker">
           <xsd:selector xpath="state"/>
           <xsd:field xpath="@stateAbbr"/>
       </xsd:unique>
   </xsd:element>
   <xsd:complexType name="regionInfo">
       <xsd:sequence>
           <xsd:element name="regionName" type="xsd:string"/>
           <xsd:element name="salesPerson" type="salesPersonInfo" maxOccurs="unbounded">
           </xsd:element>
       </xsd:sequence>
       <xsd:attribute name="regionId" type="xsd:long" use="required"/>
   </xsd:complexType>
   <xsd:complexType name="salesPersonInfo">
       <xsd:sequence>
           <xsd:element name="firstName" type="xsd:string"/>
           <xsd:element name="lastName" type="xsd:string"/>
           <xsd:element name="regionHead" minOccurs="0" maxOccurs="1">
               <xsd:complexType>
                   <xsd:attribute name="fk_salesPersonId" type="xsd:long" use="required"/>
               </xsd:complexType>
           </xsd:element>
       </xsd:sequence>
       <xsd:attribute name="salesPersonId" type="xsd:long" use="required"/>
   </xsd:complexType>


</xsd:schema>

File insuranceXSD.xsd

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE stylesheet [<!ENTITY space "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> </xsl:text>"> <!ENTITY dblspace "

">]> <!-- The namespace attribute above is only necessary for XML parsers using the MSXML parser--< <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

   <xsl:key name="regionHead" match="salesPerson" use="@salesPersonId"/>
   <xsl:output method="html"/>
   <xsl:template match="insuranceInfo">
   <xsl:for-each select="state">
   <xsl:variable name = "state"><xsl:value-of select="stateName"/></xsl:variable>
       <xsl:for-each select="region">
           			<table>
               <tr style="background-color:#6495ED;font-weight:bold">
                   <td colspan="3">
                       State Name: <xsl:value-of select="$state"/>
Region Id: <xsl:value-of select="@regionId"/>
Region Name: <xsl:value-of select="regionName"/>
</td> </tr> <tr style="background-color:#F0E68C;font-weight:bold"> <td>Sales Person Id</td><td>Sales Person Name</td><td>Region Head</td> </tr> <xsl:for-each select="salesPerson"> <tr style="background-color:#D3D3D3"> <td><xsl:value-of select="@salesPersonId"/></td> <td><xsl:value-of select="firstName"/>&space;<xsl:value-of select="lastName"/></td> <xsl:choose> <xsl:when test="regionHead"> <td> <xsl:value-of select="key('regionHead',regionHead/@fk_salesPersonId)/firstName"/>&space; <xsl:value-of select="key('regionHead',regionHead/@fk_salesPersonId)/lastName"/></td> </xsl:when> <xsl:otherwise> <td style="background-color:#FF0000;font-weight:bold">Region Head</td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table>
</xsl:for-each>&dblspace; </xsl:for-each> </xsl:template> <xsl:template match="/"> <html> <head> <title>Insurance Regions</title> </head> <body style="background-color:darkblue"> <xsl:apply-templates select = "insuranceInfo"/> </body> </html> </xsl:template>

</xsl:stylesheet>

File insuranceXSL.xsl

  • A restaurant has many appetizers on its menu. A restaurant may also create a sampler dish which is a conglomeration of smaller portions of select appetizers. Create a schema to describe this relationship. Create an XML document and populate it with enough appetizers to create at least two sampler groups. Make sure the XML document is well formed and valid.

Answer:

<?xml version="1.0" encoding="UTF-8"?>

&lt!--

   Document   : appetizerXML.xml
   Created on : March 28, 2004, 9:46 PM
   Author     : Jess Russell
   Description:
       Purpose of the document follows.

-->

<appetizers xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'

 xsi:noNamespaceSchemaLocation='file:/Users/russell/Courses/MIST7700/XML/ch6-assn/appetizersXSD.xsd'>
   <appetizer appetizerId="1">
       <appetizerName>Buffalo Wings</appetizerName>
       <appetizerPrice>7.99</appetizerPrice>
   </appetizer>
   <appetizer appetizerId="2">
       <appetizerName>Mozerella Sticks</appetizerName>
       <appetizerPrice>4.99</appetizerPrice>
   </appetizer>
   <appetizer appetizerId="3">
       <appetizerName>Potato Skins</appetizerName>
       <appetizerPrice>5.99</appetizerPrice>
   </appetizer>
   <appetizer appetizerId="4">
       <appetizerName>Onion Blossom</appetizerName>
       <appetizerPrice>7.99</appetizerPrice>
   </appetizer>
   <appetizer appetizerId="5">
       <appetizerName>Chips and Salsa</appetizerName>
       <appetizerPrice>2.49</appetizerPrice>
   </appetizer>
   <appetizer appetizerId="6">
       <appetizerName>Pot Stickers</appetizerName>
       <appetizerPrice>5.99</appetizerPrice>
   </appetizer>
   <appetizer appetizerId="7">
       <appetizerName>Wings and Things Sampler</appetizerName>
       <appetizerPrice>12.99</appetizerPrice>
       <samplerAppetizer fk_samplerAppetizerId="1"/>
       <samplerAppetizer fk_samplerAppetizerId="2"/>
       <samplerAppetizer fk_samplerAppetizerId="3"/>
   </appetizer>
   <appetizer appetizerId="8">
       <appetizerName>Ultimate Sampler</appetizerName>
       <appetizerPrice>19.99</appetizerPrice>
       <samplerAppetizer fk_samplerAppetizerId="1"/>
       <samplerAppetizer fk_samplerAppetizerId="2"/>
       <samplerAppetizer fk_samplerAppetizerId="3"/>
       <samplerAppetizer fk_samplerAppetizerId="4"/>
       <samplerAppetizer fk_samplerAppetizerId="5"/>
       <samplerAppetizer fk_samplerAppetizerId="6"/>
   </appetizer>

</appetizers>

File appetizerXML.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--

   Document   : appetizerXML.xml
   Created on : March 28, 2004, 9:46 PM
   Author     : Jess Russell
   Description:
       Purpose of the document follows.

-->

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

   <xsd:element name="appetizers">
       <xsd:complexType>
           <xsd:sequence>
               <xsd:element name="appetizer" type="appetizerInfo" minOccurs="0" maxOccurs="unbounded">
               </xsd:element>
           </xsd:sequence>
       </xsd:complexType>
   <xsd:unique name="appetizerChecker">
       <xsd:selector xpath="appetizer"/>
       <xsd:field xpath="@appetizerId"/>
   </xsd:unique>
   <xsd:key name="appetizerKey">
       <xsd:selector xpath="appetizer"/>
       <xsd:field xpath="@appetizerId"/>
   </xsd:key>
   <xsd:keyref name="samplerAppetizerKey" refer="appetizerKey">
       <xsd:selector xpath="samplerAppetizer"/>
       <xsd:field xpath="@fk_samplerAppetizerId"/>
   </xsd:keyref>
   </xsd:element>
   <xsd:complexType name="appetizerInfo">
       <xsd:sequence>
           <xsd:element name="appetizerName"/>
           <xsd:element name="appetizerPrice" type="xsd:decimal"/>
           <xsd:element name="samplerAppetizer" minOccurs="0" maxOccurs="unbounded">
               <xsd:complexType>
                   <xsd:attribute name="fk_samplerAppetizerId" type="xsd:long" use="required"/>
               </xsd:complexType>
           </xsd:element>
       </xsd:sequence>
       <xsd:attribute name="appetizerId" type="xsd:long" use="required"/>
   </xsd:complexType>

</xsd:schema>

File appetizerXSD.xsd

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.