Comment utiliser XSD ?
- Page précédente Tutoriel XSD
- Page suivante XSD <schema>
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
Voici un exemple de fichier DTD nommé "note.dtd", qui définit les éléments du document XML ci-dessus :
<!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>
La définition du module note comporte quatre éléments enfants : "to, from, heading, body".
Les lignes 2 à 5 définissent le type des éléments to, from, heading, body comme "#PCDATA".
Schema XML
L'exemple suivant est un fichier XML Schema nommé "note.xsd" qui définit les éléments du document XML suivant :
<?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 un type composite car il contient d'autres éléments. D'autres éléments (to, from, heading, body) sont de types simples car ils ne contiennent pas d'autres éléments. Vous apprendrez plus sur les types composés et simples dans les chapitres suivants.
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>
- Page précédente Tutoriel XSD
- Page suivante XSD <schema>