XML - Managing Data Exchange/Recursive relationships/Answers

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Exercises 2a[edit | edit source]

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

&lt;!--
    Document   : appetizerXML.xml
    Created on : March 28, 2004, 9:46 PM
    Author     : Jess Russell
    Description:
        Purpose of the document follows.
--&gt;

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

&lt;!--
    Document   : relayXSD.xml
    Created on : March 28, 2004, 9:46 PM
    Author     : Jess Russell
    Description:
        Purpose of the document follows.
--&gt;

<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.
--&gt;

<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:key name="name referenced from within XSL file" match="element in XML file containing key" use="attribute or sub-element containing actual value of key "-->
	<!--	key() function use:
		key('predecessor',2) will return the nodes contained by the president element with attribute: primaryKey="2"
		Since a node list is returned, <xsl:value-of select="key('predecessor',2)/firstName"/> will output John
	-->
	<xsl:output method="html"/>
	<xsl:template match="/">
        <html>
        <head>
        <style>
        BODY {background-color:darkblue}
        </style>
        </head>
        <body>
            &lt;table style="background-color:gray; color:darkblue;margin-left:auto; margin-right:auto"&gt;
                &lt;tr style="background-color:lightblue;font-weight:bold">
                        &lt;td&gt;Runner First Name&lt;/td&gt;
                        &lt;td&gt;Runner Last Name&lt;/td&gt;
                        &lt;td&gt;Predecessor First Name&lt;/td&gt;
                        &lt;td&gt;Predecessor Last Name&lt;/td&gt;
                &lt;/tr&gt;
                <xsl:for-each select="//runner">
                    &lt;tr style="background-color:salmon">
                        &lt;td&gt;
                            <xsl:value-of select="firstName"/>
                        &lt;/td&gt;
                        &lt;td&gt;
                            <xsl:value-of select="lastName"/>
                        &lt;/td&gt;
                        <xsl:choose>
                            <!--XSL if-else structure-->
                            <xsl:when test="key('predecessor',predecessor/@foreignKey)">
                                <!-- if there is a predecessor-->
                                &lt;td&gt;
                                    <xsl:value-of select="key('predecessor',predecessor/@foreignKey)/firstName"/>
                                &lt;/td&gt;
                                &lt;td&gt;
                                    <xsl:value-of select="key('predecessor',predecessor/@foreignKey)/lastName"/>
                                &lt;/td&gt;
                            </xsl:when>
                            <xsl:otherwise>
                                <!-- else-->
                                &lt;td colspan="2" style="text-align:center">This Runner started the race&lt;/td&gt;
                            </xsl:otherwise>
                        </xsl:choose>
                    &lt;/tr&gt;
                </xsl:for-each>
            &lt;/table&gt;            </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"?>

&lt;!--
    Document   : insuranceXML.xml
    Created on : March 21, 2004, 11:26 PM
    Author     : russell
    Description:
        Purpose of the document follows.
--&gt;

<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.
--&gt;

<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 "<br /><br />">]>
&lt;!-- The namespace attribute above is only necessary for XML parsers using the MSXML parser--&lt;
<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">
            			&lt;table&gt;
                &lt;tr style="background-color:#6495ED;font-weight:bold">
                    &lt;td colspan="3">
                        State Name: <xsl:value-of select="$state"/><br />
                        Region Id: <xsl:value-of select="@regionId"/><br />
                        Region Name: <xsl:value-of select="regionName"/><br />
                    &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr style="background-color:#F0E68C;font-weight:bold">
                    &lt;td&gt;Sales Person Id&lt;/td&gt;&lt;td&gt;Sales Person Name&lt;/td&gt;&lt;td&gt;Region Head&lt;/td&gt;
                &lt;/tr&gt;
                <xsl:for-each select="salesPerson">
                &lt;tr style="background-color:#D3D3D3">
                    &lt;td&gt;<xsl:value-of select="@salesPersonId"/>&lt;/td&gt;
                    &lt;td&gt;<xsl:value-of select="firstName"/>&space;<xsl:value-of select="lastName"/>&lt;/td&gt;
                    <xsl:choose>
                            <xsl:when test="regionHead">
                                &lt;td&gt;
                                    <xsl:value-of select="key('regionHead',regionHead/@fk_salesPersonId)/firstName"/>&space;
                                    <xsl:value-of select="key('regionHead',regionHead/@fk_salesPersonId)/lastName"/>&lt;/td&gt;
                            </xsl:when>
                            <xsl:otherwise>
                                &lt;td style="background-color:#FF0000;font-weight:bold">Region Head&lt;/td&gt;
                            </xsl:otherwise>
                    </xsl:choose>
                &lt;/tr&gt;				
                </xsl:for-each>		
            &lt;/table&gt;
            <br />
        </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.
--&gt;

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

&lt;!--
    Document   : appetizerXML.xml
    Created on : March 28, 2004, 9:46 PM
    Author     : Jess Russell
    Description:
        Purpose of the document follows.
--&gt;

<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