XML - Managing Data Exchange/A single entity/Answers
Appearance
Single-Entity CHAPTER => A Single Entity
Single-Entity EXERCISES => Exercises
Class Example - Wine Store
[edit | edit source]XML Document
[edit | edit source]
<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 Schema
[edit | edit source]name the schema file 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 Stylesheet
[edit | edit source]<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> |
Exercise 1
[edit | edit source]- A museum problem
- a. Create an XML schema to describe a museum entity that has the following attributes: museum name, date established, address and URL. Check that it is well-formed and valid.
- b. Using the schema, create an XML document and populate it with data for two museums. Check that it is well-formed and valid.
- c. Write an XML stylesheet to display the name of the museum, date established and the URL. Check that it is well-formed and valid.
- d. Write a Java program to parse the XML document and list each museum and its address.
Answer=
1a. Submitted by Madeleine on February 3, 2004, 5:48 PM
XML Schema
[edit | edit source]<?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. Submitted by Madeleine on February 3, 2004, 5:48 PM
XML Document
[edit | edit source]
<?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. Submitted by Madeleine on February 3, 2004, 5:48 PM
XML Stylesheet
[edit | edit source]
<?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> |
Exercise 2
[edit | edit source]Solution to Nr. 2:
Exercise 3
[edit | edit source]Submitted by Chris Collins on March 28, 2005
XML Schema
[edit | edit source]--Exercise 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 Document
[edit | edit source]--Exercise 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 Stylesheet
[edit | edit source]--Exercise 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> |
Single-Entity CHAPTER => A Single Entity
Single-Entity EXERCISES => Exercises