Latest web development tutorials

How to use XML Schema

XML documents against a DTD or XML Schema can be referenced.


A simple XML document:

Look at this, called "note.xml" XML document:

<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


DTD file

The following example is the DTD file named "note.dtd", its above the XML document ( "note.xml") elements are defined:

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

The first line defines the note element has four sub-elements: "to, from, heading, body".

2-5 line defines the to, from, heading, body type element is "#PCDATA".


XML Schema

The following example is the XML Schema file called "note.xsd", which defines the XML document above ( "note.xml") elements:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.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>

note element is a complex type, because it contains other child elements. Other elements (to, from, heading, body) is a simple type, because they do not contain other elements. You will learn more about the types of complex and simple types of knowledge in the following sections.


A reference to the DTD

This file contains a reference to a DTD:

<?xml version="1.0"?>

<!DOCTYPE note SYSTEM
"http://www.w3schools.com/dtd/note.dtd">

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


A reference to the XML Schema

This file contains a reference to the XML Schema:

<?xml version="1.0"?>

<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>