==== DTD (Document Type Definition) ====
In simple cases, **DTD** is used to describe the syntax of a data structure stored in XML. This DTD can be saved in a file with a ''.dtd'' extension, but it can also be part of the XML document itself.
=== Basic Properties of DTD ==
- Allows for simple **syntactic validation**.
- A descriptive language similar to **extended Backus-Naur form**.
- You can define structures, required sequences, type constraints, and cardinality.
If an XML document has a document type declaration, it must be indicated in the file with a special declaration starting with ``. The type declaration can be **internal** or **external** (referenced via **URI** or a **file**).
=== Internal declaration ===
=== External URI ===
=== External file ===
The DTD defines the structure and rules that an XML document must follow to be considered valid.
==== DTD Through Examples ====
Here is a DTD file named ''message_syntax.dtd'' and the corresponding XML on the right:
Hello XML
In the XML structure, a **comma** indicates the required order of elements:
]>
Kiss Janos
Gipsz Jakab
The **pipe symbol `|`** expresses an **either-or** relationship. For example, the storage device can be either a **pendrive** or an **SSD**, but not both:
]>
64MB
There are three ways to express **frequency**:
^ Symbol ^ Meaning ^
| ''+'' | The element appears at least once. |
| ''*'' | The element can appear any number of times, including zero. |
| ''?'' | The element appears zero or one time. |
==== Example: A music CD contains **tracks**: ====
]>
Song 1
Song 2
----
==== Example: An album has at least one title, followed by at least one **track title** and **duration**: ====
]>
**Syntactically correct XML**:
Title 1
Subtitle
Track Title 1
3.42
Track Title 2
2.32
----
==== Example: A library may contain **books** (zero or more)====
]>
**Syntactically correct XML**:
Orwell, George
1984
Brown, Dan
The Da Vinci Code
===== Defining Attributes in DTD =====
If a class has an attribute such as "number of students," it can be specified as follows:
Attributes can be:
- **#IMPLIED**: not mandatory
- **#REQUIRED**: mandatory
- **#FIXED**: fixed value
=== Specifying a Default Attribute Value ==
Example:
or
=== Enumerated Values ===
**Syntax**:
**DTD Example**:
**XML Example**:
or
==== Complex DTD Example for a Hypothetical Mail Processing System ====
**Task**: Provide an XML example that satisfies the following DTD:
=== A Possible XML for the given DTD ===
alice@usa.com
bob@jp.com
Hash code
John Doe
1234
Miskolc
Hungary
Happy Name Day
This provides an example of how attributes, including mandatory, optional, and default values, can be defined in a DTD and represented in an XML document.