Comment utiliser XSD ?

Un document XML peut faire référence à un DTD ou un XML Schema.

Un document XML simple :

Voyons le document XML nommé "note.xml" :

<?xml version="1.0"?>
<note>
<to>George</to>
<from>John</from>
<heading>Rappel</heading>
<body>N'oubliez pas la réunion !</body>
</note>

Fichier DTD

L'exemple suivant est un fichier DTD nommé "note.dtd", qui définit les éléments du document XML précédent :

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

La ligne 1 définit que l'élément 'note' a quatre éléments fils : "to, from, heading, body".

Les lignes 2 à 5 définissent les types des éléments to, from, heading, body comme "#PCDATA".

XML Schema

L'exemple suivant est un fichier XML Schema nommé "note.xsd", qui définit les éléments de ce document XML :

<?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>

L'élément 'note' est de type composite car il contient d'autres éléments fils. Les autres éléments (to, from, heading, body) sont de type simple car ils ne contiennent pas d'autres éléments. Vous apprendrez plus sur les types composés et simples dans les sections suivantes.

Références à DTD

Ce fichier contient des références à DTD :

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "http://www.codew3c.com/dtd/note.dtd">
<note>
<to>George</to>
<from>John</from>
<heading>Rappel</heading>
<body>N'oubliez pas la réunion !</body>
</note>

Références à XML Schema

Ce fichier contient des références à 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>Rappel</heading>
<body>N'oubliez pas la réunion !</body>
</note>