XML Schemas

Examples

<?xml version="1.0"?>

<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/Users/ynonperek/src/java/android/XMLExamples1/book.xsd">

  <Title>Purple Cow</Title>
  <Author>Seth Goddin</Author>
  <Publisher>Portfolio Hardcover</Publisher>
  <Year>2003</Year>
  <Reviews>
    <Review>Great Book, Changed My Life</Review>
    <Review>Not so good, don't buy it</Review>
  </Reviews>
</Book>
	
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:annotation>
    <xs:documentation>
      A Simple XML Schema
    </xs:documentation>
  </xs:annotation>
  
  <xs:element name="Title" type="xs:string" />
  <xs:element name="Author" type="xs:string" />
  <xs:element name="Publisher" type="xs:string" />
  <xs:element name="Year" type="xs:int" />

  <xs:element name="Review" type="xs:string" />

  <xs:element name="Reviews">
    <xs:complexType>    	
      <xs:sequence>
        <xs:element ref="Review" minOccurs="1" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  

  <xs:element name="Book">
    <xs:complexType>
      <xs:sequence>
      	<xs:element ref="Title" />
      	<xs:element ref="Author" />
      	<xs:element ref="Publisher" />      	
        <xs:element ref="Year"></xs:element>
        <xs:element ref="Reviews"></xs:element>
      </xs:sequence>
      <xs:attribute name="Country" type="xs:string" />
    </xs:complexType>
  </xs:element>


</xs:schema>
<?xml version="1.0" ?>

<weather xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/Users/ynonperek/src/java/android/XMLExamples1/weather.xsd">

	<city size="large">
		<name>Tel Aviv</name>
		<status>Too hot</status>		
	</city>
	
	<city size="x-large">
		<name>New York</name>
		<status>Cold</status>		
	</city>
	
	<city size="small">
		<name>Jerusalem</name>
		<status>Holy</status>
	</city>
</weather>

<?xml version="1.0" ?>

<weather xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/Users/ynonperek/src/java/android/XMLExamples1/weather.xsd">

	<city size="large">
		<name>Tel Aviv</name>
		<status>Too hot</status>		
	</city>
	
	<city size="x-large">
		<name>New York</name>
		<status>Cold</status>		
	</city>
	
	<city size="small">
		<name>Jerusalem</name>
		<status>Holy</status>
	</city>
</weather>

Exercises

For each of the following, write a schema that makes it valid:

<?xml version="1.0" ?>

<home xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/Users/ynonperek/src/java/android/XMLExamples1/ex1/home.xsd">
	<room type="livingroom">
		<doors>2</doors>
		<windows>3</windows>
		<floor decorated="yes">40</floor>
		<electricity>
			<lights>5</lights>
			<switches>3</switches>
		</electricity>
	</room>
	
	<room type="bedroom">
		<doors>1</doors>
		<windows>1</windows>
		<floor>10</floor>
		<electricity>
			<lights>3</lights>
			<switches>5</switches>
		</electricity>
	</room>
	
</home>
<menu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/Users/ynonperek/src/java/android/XMLExamples1/ex2/food.xsd">
	<food>
		<plate price="38">Pasta</plate>
		<plate price="44">Breakfast</plate>
		<plate price="34">Salad</plate>
	</food>
	
	<drinks>
		<cup price="10">Tea</cup>
		<cup price="18">Coffee</cup>
		<cup price="15">Ice Tea</cup>
	</drinks>
	
	<deserts>
		<yummie price="42">Cake</yummie>
		<yummie price="44">Ice Cream</yummie>		
	</deserts>
</menu>

<phonebook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/Users/ynonperek/src/java/android/XMLExamples1/ex3/phonebook.xsd">
	<contact source="sim">
		<name>Leo H. Goff</name>
		<address>
			<line>2238 Coulter Lane</line>
			<line>Richmond, VA 23222 </line>
		</address>
		
		<comments>
			<comment>No Comment</comment>			
		</comments>
	</contact>
	
	<contact source="google">
		<name>Guang Tang</name>
		<address>
			<line>1049 Bond Street</line>
			<line>Providence, RI 02908 </line>
		</address>
		<phone>401-812-0797</phone>		
	</contact>
	
	<contact source="facebook">
		<name>Florence Aupry</name>
		<address>
			<line>4788 Post Avenue</line>
			<line>Biwabik, MN 55708 </line>
		</address>
		<phone>218-865-8420</phone>
	</contact>
	
	<contact source="facebook">
		<name>Fanchon Vachon</name>
		<address>
			<line>1097 Hawks Nest Lane</line>
			<line>Maryland Heights, MO 63043</line>
		</address>
	</contact>
	
	<contact source="sim">
		<name>Smid Andor</name>
		<address>
			<line>Via Alessandro Manzoni, 67</line>
			<line>27050-Corvino San Quirico PV </line>			
		</address>
		<email>SmidAndor@teleworm.com</email>		
	</contact>
</phonebook>

Link To Solutions

course: