XSD ¿Cómo usar?
Un documento XML puede hacer referencia a un DTD o a un XML Schema.
Un documento XML simple:
Consulte este documento XML llamado "note.xml":
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Archivo DTD
El siguiente ejemplo es un archivo DTD llamado "note.dtd" que define los elementos del documento XML ("note.xml") anterior:
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
La línea 1 define que el elemento note tiene cuatro subelementos: "to, from, heading, body".
Las líneas 2-5 definen que los tipos de los elementos to, from, heading, body son "#PCDATA".
XML Schema
El siguiente ejemplo es un archivo XML Schema llamado "note.xsd" que define los elementos del documento XML ("note.xml") anterior:
<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>
El elemento note es de tipo compuesto porque contiene otros subelementos. Los otros elementos (to, from, heading, body) son de tipo simple porque no contienen otros elementos. Aprenderá más sobre los tipos compuestos y los tipos simples en los siguientes capítulos.
Referencia al DTD
Este archivo contiene una referencia al DTD:
<!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>
Referencia al XML Schema
Este archivo contiene una referencia al XML Schema:
<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>