XSD Comment l'utiliser ?


Les documents XML peuvent référencer une DTD ou un schéma XML.


Un document XML simple :

Regardez ce document XML nommé "note.xml" :

<?xml version="1.0"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Rappel</heading>
  <body>Ne m'oubliez pas ce week-end !</body>
</note>


Fichier DTD

L'exemple suivant est un fichier DTD nommé "note.dtd", qui définit les éléments du document XML ("note.xml") ci-dessus :

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

La ligne 1 définit l'élément note comme ayant quatre éléments enfants : "to, from, heading, body".

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


Schéma XML

L'exemple suivant est un fichier de schéma XML nommé "note.xsd", qui définit les éléments du document XML ("note.xml") ci-dessus :

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

L'élément note est de type complexe car il contient d'autres éléments enfants. Les autres éléments (to, from, heading, body) sont de type simple car ils ne contiennent pas d'autres éléments. Vous en apprendrez plus sur les types complexes et les types simples dans les chapitres suivants.


Référence à la DTD

Ce fichier contient une référence à la DTD :

<?xml version="1.0"?>

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

<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Rappel</heading>
  <body>Ne m'oubliez pas ce week-end !</body>
</note>


Référence au schéma XML

Ce fichier contient une référence au schéma XML :

<?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>Rappel</heading>
  <body>Ne m'oubliez pas ce week-end !</body>
</note>
Autres extensions