Dtd Elements in XML
DTD Elements in XML
What are DTD Elements?
Elements are the building blocks of an XML document.
In DTD, elements define the structure of the XML by specifying:
What elements can appear
The order of child elements
Whether children are optional or repeatable
Whether an element contains text or other elements
Declaring Elements in DTD
<!ELEMENT element-name content-model>element-name: The name of the element.content-model: Defines what the element contains.
Content Model Types
| Content Model | Description | Example |
|---|---|---|
EMPTY | Element contains no content | <!ELEMENT br EMPTY> |
ANY | Element can contain any content | <!ELEMENT note ANY> |
(#PCDATA) | Element contains parsed character data (text) | <!ELEMENT title (#PCDATA)> |
Sequence (a, b, c) | Element contains child elements in order | <!ELEMENT book (title, author)> |
| Choice `(a | b | c)` |
| Mixed Content `( #PCDATA | a | b )*` |
Occurrence Indicators
| Symbol | Meaning | Example |
|---|---|---|
? | Optional (0 or 1 occurrence) | (author)? |
* | Zero or more occurrences | (chapter)* |
+ | One or more occurrences | (item)+ |
Examples
1. Simple Element with Text
<!ELEMENT title (#PCDATA)>Means <title> contains text only.
2. Element with Sequence of Child Elements
<!ELEMENT book (title, author, year)>Means <book> must contain <title>, then <author>, then <year>, in that order.
3. Element with Choice
<!ELEMENT status (new | pending | done)>Means <status> contains one of the three literal values.
4. Element with Mixed Content
<!ELEMENT paragraph (#PCDATA | bold | italic)*><!ELEMENT bold (#PCDATA)><!ELEMENT italic (#PCDATA)>Means <paragraph> contains text plus zero or more <bold> or <italic> elements.
5. Empty Element
<!ELEMENT br EMPTY>Means <br> is an empty element with no content.
Summary
| DTD Element Syntax | Purpose |
|---|---|
<!ELEMENT name EMPTY> | Element has no content |
<!ELEMENT name ANY> | Element can contain anything |
<!ELEMENT name (#PCDATA)> | Element contains text only |
<!ELEMENT name (a, b, c)> | Element contains child elements in sequence |
| `<!ELEMENT name (a | b)>` |
| `<!ELEMENT name (#PCDATA | a |
If you want, I can help you build a full DTD for your XML document or show how to validate XML using these element declarations!