Dokumen XML dapat memiliki referensi ke DTD atau ke Skema XML.
Section Artikel
Lihat dokumen XML sederhana ini yang disebut “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>
Contoh berikut adalah file DTD bernama “note.dtd” yang mendefinisikan elemen dokumen XML di atas (“note.xml”):
<!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>
Baris pertama mendefinisikan elemen note memiliki empat elemen children: “to, from, heading, body”.
Baris 2-5 mendefinisikan to, from, heading, elemen isi dengan tipe “#PCDATA”.
Contoh berikut adalah file Skema XML bernama “note.xsd” yang mendefinisikan elemen dokumen XML di atas (“note.xml”):
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.w3schools.com" xmlns="https://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>
Elemen note adalah tipe kompleks karena mengandung elemen lain. Elemen lainnya (to, from, heading, body) adalah tipe sederhana karena tidak mengandung elemen lain.
Dokumen XML ini memiliki referensi ke DTD:
<?xml version="1.0"?> <!DOCTYPE note SYSTEM "https://www.w3schools.com/xml/note.dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
Dokumen XML ini memiliki referensi ke Skema XML:
<?xml version="1.0"?> <note xmlns="https://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.w3schools.com/xml note.xsd"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>