跳转到内容

XML - 管理数据交换/递归关系/答案

来自 Wikibooks,开放世界中的开放书籍

练习 2a

[编辑 | 编辑源代码]
  • 创建一个描述接力赛中一个队的模式。该信息需要描述比赛中的跑步者以及跑步者从谁那里接过接力棒。创建一个包含此模式的示例数据的 XML 文件,并用至少四个跑步者填充它。确保 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;

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

文件 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>

文件 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>

文件 relayXSL.xsl

  • 每个州都有许多人寿保险销售人员,每个州的区域(北、南、东、西)都有一个区域主管销售人员。销售人员只能在一个区域销售,并且只能有一个主管销售人员。创建一个模式来描述这种关系。创建一个 XML 文档并用两个州的数据填充它。确保 XML 文档格式良好且有效。每个州应至少有两个区域的数据,每个区域应至少有三个销售人员,包括区域主管。每个销售人员应通过一个数字唯一标识。每个州应通过其两个字母缩写唯一标识。创建一个 XSL 样式表来显示这些数据,每个州有一个单独的表格。如果销售人员是区域主管,则应说明这一点。在样式表中创建一个名为 doublespace 的实体,它等效于两个回车符。使用新实体在两个表格之间创建两行空白。

答案

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

文件 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>

文件 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>

文件 insuranceXSL.xsl

  • 一家餐厅在其菜单上有许多开胃菜。餐厅还可以创建一个拼盘,它是由精选开胃菜的小份组合而成的。创建一个模式来描述这种关系。创建一个 XML 文档并用足够多的开胃菜填充它,以创建至少两个拼盘组。确保 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;

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

文件 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>

文件 appetizerXSD.xsd

华夏公益教科书