Εισαγωγή στο DTD

Document Type Definition (DTD) can define the building blocks of a valid XML document. It uses a series of valid elements to define the structure of the document.

DTD can be declared line by line in the XML document or as an external reference.

Internal DOCTYPE declaration

If the DTD is included in your XML source file, it should be wrapped in a DOCTYPE declaration with the following syntax:

<!DOCTYPE root element [element declarations]>

An XML document instance with DTD (open in IE5 or higher versions and select View Source):

<?xml version="1.0"?>
<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from    (#PCDATA)>
  <!ELEMENT heading (#PCDATA)>
  <!ELEMENT body    (#PCDATA)>
]>
<note>
  <to>George</to>
  <from>John</from>
  <heading>Μνεία</heading>
  <body>Μη ξεχνάτε την συνάντηση!</body>
</note>

Open this XML file in your browser and select the "View Source" command.

The above DTD explanation is as follows:

!DOCTYPE note [ (Line 2) Define this document as note document type.

!ELEMENT note (Line 3) Definition note The element has four elements: "to, from, heading, body"

!ELEMENT to (Line 4) Definition to The element is of type "#PCDATA"

!ELEMENT from (Line 5) Definition from The element is of type "#PCDATA"

!ELEMENT heading (Line 6) Definition heading The element is of type "#PCDATA"

!ELEMENT body (Line 7) Definition body The element is of type "#PCDATA"

External document declaration

If the DTD is located outside the XML source file, it should be enclosed in a DOCTYPE definition with the following syntax:

<!DOCTYPE ρίζα Εлемент SYSTEM "onoma_arxeiou">

Αυτό το αρχείο XML είναι το ίδιο με το παραπάνω αρχείο XML, αλλά έχει ένα εξωτερικό DTD: (Άνοιξε στο IE5και επιλέξτε την εντολή "Εμφάνιση Κώδικα Πηγής"。)

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>George</to>
<from>John</from>
<heading>Μνεία</heading>
<body>Μη ξεχνάτε την συνάντηση!</body>
</note>

Αυτό είναι το αρχείο "note.dtd" που περιέχει το DTD:

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

Γιατί να χρησιμοποιήσουμε το DTD;

Με το DTD, κάθε αρχείο XML μπορεί να φέρει μια περιγραφή της δικής του μορφής.

Με το DTD, οι ανεξάρτητοι οργανισμοί μπορούν να χρησιμοποιούν συνεπώς ένα πρότυπο DTD για την ανταλλαγή δεδομένων.

Και η εφαρμογή σας μπορεί να χρησιμοποιήσει ένα πρότυπο DTD για να ελέγξει τα δεδομένα που λαμβάνει από το εξωτερικό.

Μπορείτε επίσης να χρησιμοποιήσετε το DTD για να ελέγξετε τα δεδομένα σας.