411 REFERENCE · Wikibooks
XML - Managing Data Exchange/A single entity/Answers
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Single-Entity CHAPTER => A Single Entity
Single-Entity EXERCISES => Exercises
Class Example - Wine Store
XML Document
<wineStore
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation='wine.xsd'>
1
Ravenswood
Zinfandel
2003
United States
Sonoma County, California
12.50
20.75
35
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.
2
Yalumba
Shiraz
2001
Australia
Barossa Valley
8.00
15.95
17
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.
3
Matariki
Sauvignon Blanc
2004
New Zealand
Hawkes Bay
7.50
16.95
22
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.
4
Fonseca
Port
2003
Portugal
Douro River
70.00
100.00
12
Ripe fruit, with chocolate, blackberries and raisins.
Full-bodied and medium sweet, with velvety tannins. Finish goes on and on.
Layered and wonderful.
5
Louis Jadot
Pinot Noir
1999
France
Burgandy
37.50
65.00
6
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
</wineStore>
XML Schema
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
<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
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
<nowiki>
<?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>
</nowiki>
1b. Submitted by Madeleine on February 3, 2004, 5:48 PM
XML Document
<nowiki>
<?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>
</nowiki>
1c. Submitted by Madeleine on February 3, 2004, 5:48 PM
XML Stylesheet
<nowiki>
<?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>
</nowiki>
Exercise 2
Solution to Nr. 2:
Image:Dataflow.png
Exercise 3
Submitted by Chris Collins on March 28, 2005
XML Schema
--Exercise 3a.--
<nowiki>
<?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>
</nowiki>
XML Document
--Exercise 3b.--
<nowiki>
<?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>
</nowiki>
XML Stylesheet
--Exercise 3c.--
<nowiki>
<?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>
</nowiki>
Single-Entity CHAPTER => A Single Entity
Single-Entity EXERCISES => Exercises