documenti XML possono avere un riferimento a un DTD oa uno schema XML.
Un documento XML semplice
Guardate questo semplice documento XML chiamato "note.xml" :
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Un file DTD
L'esempio che segue è un file DTD chiamato "note.dtd" che definisce gli elementi del documento XML di cui sopra ("note.xml") :
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
La prima riga definisce l'elemento nota di avere quattro elementi figlio: "to, from, heading, body" .
Linea 2-5 definisce la a, da, voce, gli elementi del corpo per essere di tipo "#PCDATA" .
Uno schema XML
L'esempio che segue è un file XML Schema chiamato "note.xsd" che definisce gli elementi del documento XML di cui sopra ("note.xml") :
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3ii.com"
xmlns="http://www.w3ii.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'elemento nota è un complex type perché contiene altri elementi. Gli altri elementi (to, from, heading, body) sono simple types perché non contengono altri elementi. Si impara di più sui tipi semplici e complessi nei seguenti capitoli.
Un riferimento a un DTD
Questo documento XML ha un riferimento a un DTD:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM
"http://www.w3ii.com/xml/note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Un riferimento a uno schema XML
Questo documento XML ha un riferimento a uno schema XML:
<?xml version="1.0"?>
<note
xmlns="http://www.w3ii.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3ii.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>