How to Use XSD?
- Previous Page XSD Tutorial
- Next Page XSD <schema>
XML documents can refer to DTD or XML Schema.
A simple XML document:
See this XML document named "note.xml":
<?xml version="1.0"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
DTD file
The following example is a DTD file named "note.dtd", which defines the elements of the above XML document:
<!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>
The first line defines four sub-elements of the note element: "to, from, heading, body".
Lines 2-5 define the types of the to, from, heading, and body elements as "#PCDATA".
XML Schema
The following example is an XML Schema file named "note.xsd" that defines the elements of the above XML document:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.codew3c.com" xmlns="http://www.codew3c.com" elementFormDefault="qualified"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
The note element is a complex type because it contains other child elements. Other elements (to, from, heading, body) are simple types because they do not contain other elements. You will learn more about complex types and simple types in the following chapters.
References to DTD
This file contains references to DTD:
<?xml version="1.0"?> <!DOCTYPE note SYSTEM "http://www.codew3c.com/dtd/note.dtd"> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
References to XML Schema
This file contains references to XML Schema:
<?xml version="1.0"?> <note xmlns="http://www.codew3c.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.codew3c.com note.xsd"> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
- Previous Page XSD Tutorial
- Next Page XSD <schema>