跳转到内容

XML - 管理数据交换/单个实体/答案

来自维基教科书,开放世界中的开放书籍

单个实体章节 => 单个实体


单个实体练习 => 练习


示例类 - 葡萄酒商店

[编辑 | 编辑源代码]

XML 文档

[编辑 | 编辑源代码]
 <wineStore
   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
   xsi:noNamespaceSchemaLocation='wine.xsd'>
   <wine>
       <wineID>1</wineID>
       <winery>Ravenswood</winery>
       <style>Zinfandel</style>
       <vintage>2003</vintage>
       <country>United States</country>
       <region>Sonoma County, California</region>
       <cost currency='USdollars'>12.50</cost>
       <price currency='USdollars'>20.75</price>
       <inventory>35</inventory>
       <description>This 2003 Zinfandel has huge, jammy, inky, slightly porty aromas
       infused with black pepper, vanilla and hints of tar, smoke and coffee blend. 
       A very broad, intense wine with huge fruit, lots of those luscious 
       Dry Creek bing cherry and sweet plum characters.</description>
   </wine>
   <wine>
       <wineID>2</wineID>
       <winery>Yalumba</winery>
       <style>Shiraz</style>
       <vintage>2001</vintage>
       <country>Australia</country>
       <region>Barossa Valley</region>
       <cost currency='USdollars'>8.00</cost>
       <price currency='USdollars'>15.95</price>
       <inventory>17</inventory>
       <description>The palate reveals velvety, ripe fine-grained tannins, 
       harmonising well with the oak and seductively spicy fruit. 
       The finish is rich and long, with cinnamon, cloves and brambly wild fruit note.</description>
   </wine> 
   <wine>
       <wineID>3</wineID>
       <winery>Matariki</winery>
       <style>Sauvignon Blanc</style>
       <vintage>2004</vintage>
       <country>New Zealand</country>
       <region>Hawkes Bay</region>
       <cost currency='USdollars'>7.50</cost>
       <price currency='USdollars'>16.95</price>
       <inventory>22</inventory>
       <description>This wine shows ripe passionfruit and delicate tropical fruit aromas 
       and hints of gooseberry. Delicious fresh wine with a lovely balance of acid and 
       fruit sweetness. The finish is lively and lingering.</description>
   </wine>  
   <wine>
       <wineID>4</wineID>
       <winery>Fonseca</winery>
       <style>Port</style>
       <vintage>2003</vintage>
       <country>Portugal</country>
       <region>Douro River</region>
       <cost currency='USdollars'>70.00</cost>
       <price currency='USdollars'>100.00</price>
       <inventory>12</inventory>
       <description>Ripe fruit, with chocolate, blackberries and raisins. 
       Full-bodied and medium sweet, with velvety tannins. Finish goes on and on. 
       Layered and wonderful.</description>
   </wine>
   <wine>
       <wineID>5</wineID>
       <winery>Louis Jadot</winery>
       <style>Pinot Noir</style>
       <vintage>1999</vintage>
       <country>France</country>
       <region>Burgandy</region>
       <cost currency='USdollars'>37.50</cost>
       <price currency='USdollars'>65.00</price>
       <inventory>6</inventory>
       <description>Concentrated aromas of ripe black berry and dark cherry fruit are offset by 
       suggestions of violet, minerals and tobacco in this ripe, velvety wine, and carry into a long,
       silky finish underscored by firm tannins</description>
   </wine>
 </wineStore>

XML 架构

[编辑 | 编辑源代码]

将架构文件命名为 wine.xsd

 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">
   <xsd:element name="wineStore">
       <xsd:complexType>
           <xsd:sequence>
               <xsd:element name="wine" type="wineDescription" minOccurs = "1" maxOccurs="unbounded" />
           </xsd:sequence>
       </xsd:complexType>
   </xsd:element>
       <xsd:complexType name="wineDescription">
       <xsd:sequence> 
            <xsd:element name="wineID" type="xsd:integer" />
            <xsd:element name="winery" type="xsd:string" />
            <xsd:element name="style" type="xsd:string" />
            <xsd:element name="vintage" type="xsd:integer" />
            <xsd:element name="country" type="xsd:string" />
            <xsd:element name="region" type="xsd:string" />
            <xsd:element name="cost" type="xsd:decimal">
               <xsd:complexType>
                   <xsd:attribute name="currency" type="xsd:string" />
               </xsd:complexType>
            </xsd:element>   
            <xsd:element name="price" type="xsd:decimal">
               <xsd:complexType>
                   <xsd:attribute name="currency" type="xsd:string" />
               </xsd:complexType>
            </xsd:element>
            <xsd:element name="price" type="xsd:decimal" />
            <xsd:element name="inventory" type="xsd:integer" />
            <xsd:element name="description" type="xsd:string" />
        </xsd:sequence>
    </xsd:complexType>
 </xsd:schema>

XML 样式表

[编辑 | 编辑源代码]
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
      <xsl:template match="/">
        <html>
            <head>
                <title>WINE STORE</title>
            </head>
            <body bgcolor="#4B0082">
                <br /><center><H1><font face="algerian" color="#FFDAB9">WINE STORE</font></H1></center><br />
                    <xsl:apply-templates select="wineStore" /> 
            </body>
        </html>
    </xsl:template>
    <xsl:template match="wineStore">
      <xsl:for-each select="wine">
         <center>
           <table bgcolor="#F08080" border="3" cellpadding="5" width="715px">
             <tr>
               <td width="100px" align="center"><font size="7"><xsl:value-of select="wineID" /></font></td>
               <td width="350px"><font size="6"><xsl:value-of select="winery" /></font><br /><br />
               <font size="4"><xsl:value-of select="country" />
               <br />
               <xsl:value-of select="region" /></font></td>
               <td width="65px"><font size="6"><xsl:value-of select="vintage" /></font></td>
               <td width="200px"><font size="6"><xsl:value-of select="style" /></font></td>
             </tr>
             <tr>
               <td><font size="5"><xsl:text>Cost:</xsl:text><br />
               <xsl:text>$ </xsl:text><xsl:value-of select="cost" /></font></td>
               <td colspan="3" rowspan="2"><xsl:value-of select="description"  /></td>
             </tr>  
             <tr>
               <td><font size="5"><xsl:text>Price:</xsl:text><br />
               <xsl:text>$ </xsl:text><xsl:value-of select="price" /></font></td>
             </tr>
           </table>
          </center>             
         <br /><br />
        </xsl:for-each>
    </xsl:template>
  </xsl:stylesheet>


  1. 博物馆问题
  • a. 创建一个 XML 架构来描述一个博物馆实体,该实体具有以下属性:博物馆名称、成立日期、地址和 URL。检查它是否格式良好且有效。
  • b. 使用该架构,创建一个 XML 文档,并用两个博物馆的数据填充它。检查它是否格式良好且有效。
  • c. 编写一个 XML 样式表,用于显示博物馆名称、成立日期和 URL。检查它是否格式良好且有效。
  • d. 编写一个 Java 程序来解析 XML 文档,并列出每个博物馆及其地址。

答案= 1a. 由 Madeleine 于 2004 年 2 月 3 日下午 5:48 提交

XML 架构

[编辑 | 编辑源代码]
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="unqualified">

<!-- Directory -->
<xsd:element name="directory">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="museum" type="museumDetails" 
                minOccurs="1" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<!-- Museum -->
<xsd:complexType name="museumDetails">
    <xsd:sequence>
        <xsd:element name="museumName" type="xsd:string"/>
        <xsd:element name="dateEstablished" type="xsd:date"/>
        <xsd:element name="address1" type="xsd:string"/>
        <xsd:element name="address2" type="xsd:string"/>
        <xsd:element name="city" type="xsd:string"/>
        <xsd:element name="adminUnit" type="xsd:string"/>
        <xsd:element name="country" type="xsd:string"/>
        <xsd:element name="postalCode" type="xsd:string"/>
        <xsd:element name="museumURL" type="xsd:anyURI"/>
     </xsd:sequence>
</xsd:complexType>
</xsd:schema>


1b. 由 Madeleine 于 2004 年 2 月 3 日下午 5:48 提交

XML 文档

[编辑 | 编辑源代码]
<?xml version="1.0" encoding="UTF-8"?>

<directory xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xsi:noNamespaceSchemaLocation='museum.xsd'>
<museum>
    <museumName>National Museum of Natural History</museumName>
    <dateEstablished>1910-01-01</dateEstablished>
    <address1>10th Street and Constitution Avenue, NW</address1>
    <address2>Smithsonian Institute</address2>
    <city>Washington</city>
    <adminUnit>District of Columbia</adminUnit>
    <country>USA</country>
    <postalCode>20560</postalCode>
    <museumURL>http://www.mnh.si.edu/</museumURL>
</museum>
<museum>
    <museumName>Corcoran Gallery of Art</museumName>
    <dateEstablished>1897-01-01</dateEstablished>
    <address1>500 17th Street, NW</address1>
    <address2/>
    <city>Washington</city>
    <adminUnit>District of Columbia</adminUnit>
    <country>USA</country>
    <postalCode>20006</postalCode>
    <museumURL>http://www.corcoran.org/</museumURL>
</museum>
</directory>


1c. 由 Madeleine 于 2004 年 2 月 3 日下午 5:48 提交

XML 样式表

[编辑 | 编辑源代码]
<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>museum_ch2.xsl</title>
            </head>
            <body style="font-family: Arial;">
                <h2>Museum Directory</h2>
                <xsl:apply-templates select="directory"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="directory">
        <xsl:for-each select="museum">
            <xsl:text>Name: </xsl:text>
            <xsl:value-of select="museumName"/>
            <br/>
            <xsl:text>Established: </xsl:text>
            <xsl:value-of select="dateEstablished"/>
            <br/>
            <xsl:text>URL: </xsl:text>
            <xsl:value-of select="museumURL"/>
            <br/>
            <br/>
        </xsl:for-each>
    </xsl:template>   
</xsl:stylesheet>



第 2 号的解决方案



由 Chris Collins 于 2005 年 3 月 28 日提交

XML 架构

[编辑 | 编辑源代码]

--练习 3a.--

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

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

<!-- Directory -->
<xsd:element name="directory">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="fraternity" type="fraternityDetails" 
                minOccurs="1" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<!-- Fraternity -->
<xsd:complexType name="fraternityDetails">
    <xsd:sequence>
        <xsd:element name="fratName" type="xsd:string"/>
        <xsd:element name="chapterName" type="xsd:string"/>
        <xsd:element name="dateEstablished" type="xsd:integer"/>
        <xsd:element name="fratPresident" type="xsd:string"/>
        <xsd:element name="numOfMembers" type="xsd:integer"/>
        <xsd:element name="address1" type="xsd:string"/>
        <xsd:element name="city" type="xsd:string"/>
        <xsd:element name="state" type="xsd:string"/>
        <xsd:element name="country" type="xsd:string"/>
        <xsd:element name="postalCode" type="xsd:string"/>
     </xsd:sequence>
</xsd:complexType>
</xsd:schema>


XML 文档

[编辑 | 编辑源代码]

--练习 3b.--

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

<directory xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xsi:noNamespaceSchemaLocation='Fraternity.xsd'>
<fraternity>
    <fratName>Lambda Chi Alpha</fratName>
    <chapterName>Eta Pi</chapterName>
    <dateEstablished>1910</dateEstablished>
    <fratPresident>Robert Langford</fratPresident>
    <numOfMembers>86</numOfMembers>    
    <address1>1760 College Ave.</address1>
    <city>Atlanta</city>
    <state>Ga</state>
    <country>USA</country>
    <postalCode>30303</postalCode>
</fraternity>
<fraternity>
    <fratName>Sigma Phi</fratName>
    <chapterName>Alpha Omega</chapterName>
    <dateEstablished>1892</dateEstablished>
    <fratPresident>James Vanderbilt</fratPresident>
    <numOfMembers>124</numOfMembers>  
    <address1>1871 Towne Place Dr.</address1>
    <city>Austin</city>
    <state>TX</state>
    <country>USA</country>
    <postalCode>78767</postalCode>
</fraternity>
</directory>


XML 样式表

[编辑 | 编辑源代码]

--练习 3c.--

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>fraternity.xsl</title>
            </head>
            <body style="font-family: Arial;">
                <h2>Fraternity Directory</h2>
                <xsl:apply-templates select="directory"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="directory">
        <xsl:for-each select="fraternity">
            <xsl:text>Name: </xsl:text>
            <xsl:value-of select="fratName"/>
            <br/>
            <xsl:text>Established: </xsl:text>
            <xsl:value-of select="dateEstablished"/>
            <br/>
            <xsl:text>Chapter: </xsl:text>
            <xsl:value-of select="chapterName"/>
            <br/>
            <xsl:text>Membership: </xsl:text>
            <xsl:value-of select="numOfMembers"/>
            <br/>
            <xsl:text>President: </xsl:text>
            <xsl:value-of select="fratPresident"/>
            <br/>
            <br/>
        </xsl:for-each>
    </xsl:template>   
</xsl:stylesheet>


单个实体章节 => 单个实体


单个实体练习 => 练习

华夏公益教科书