Magazine
 
XML Schema

Schemas, the sender can describe the data in a way that the receiver will understand. A date like: “03-11-2004” will, in some countries, be interpreted as 3.November and in other countries as 11.March.However, an XML element
with a data type like this: <datetype=”date”>2004-03-11</date> ensures a mutual understanding of the content,
because the XML data type “date” requires the format “YYYY-MM-DD”.

Designing XML Schema

XML documents can have a reference to a DTD or to an XML Schema.

A Simple XML Document

Look at this simple XML document called “Email. xml”:

<?xml version=”1.0"?>
<E-mail>
<To>Rohan</To>
<From>Amit</From>
<Subject>Surprise....</Subject>
<Body>Be ready for a cruise...</Body>
</E-mail>
XML Schema
The following example is a XML Schema file called
“E-mail.xsd” that defines the elements of the
XML document above (“E-mail.xml”):
<?xml version=”1.0"?>
<xs:schema xmlns:xs=”http://
www.w3.org/2001/XMLSchema”
targetNamespace=”http://
www.roseindia.net”
xmlns=”http://www.roseindia.net”
elementFormDefault=”qualified”>
<xs:element name=”E-mail”>
<xs:complexType>
<xs:sequence>
<xs:element name=”To” type=”xs:string”/
>

 

<xs:element name=”From”
type=”xs:string”/>
<xs:element name=”Subject”
type=”xs:string”/>
<xs:element name=”Body”
type=”xs:string”/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

We will discuss the building blocks of this schema latter in this section further.

Add a reference to the above-declared XML document

Now this XML document (E-mail.xml) has a reference to above-declared XML Schema (Email. xsd).

<?xml version=”1.0"?>
<E-mail
xmlns=”http://www.roseindia.net”
xmlns:xsi=”http://www.w3.org/2001/
XMLSchema”
xsi:schemaLocation=”http://
www.roseindia.net/Schema E-mail.xsd”>
<To>Rohan</To>
<From>Amit</From>
<Subject>Surprise....</Subject>
<Body>Be ready for a cruise...</Body>
</E-mail>

In the above xml document xmlns declares the XML namespaces.

Save E-mail.xml and E-mail.xsd in the same
location. Open the file E-mail.xml in a webbrowser.
You will see the following:

Feb  2008 | Java Jazz Up | 17
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 ,

Download PDF