DTD Elements
Defining XML document structure with elements
📦 DTD Elements
Elements are the main building blocks of XML documents. DTD element declarations define what content each element can contain, including text, child elements, or both types combined.
<!-- Basic element declaration -->
<!ELEMENT elementName (content-model)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT book (title,author,price)>
Element Types
Empty Elements
Elements with no content
<!ELEMENT br EMPTY>
<br/>
Text Elements
Elements containing only text
<!ELEMENT name (#PCDATA)>
<name>John</name>
Child Elements
Elements containing other elements
<!ELEMENT person (name,age)>
<person><name>...</name></person>
Mixed Content
Elements with text and child elements
<!ELEMENT p (#PCDATA|b|i)*>
<p>Text <b>bold</b></p>
🔹 Empty Elements
Empty elements contain no content at all. They are often used for line breaks, images, or metadata tags in XML documents.
<!-- DTD Declaration -->
<!ELEMENT br EMPTY>
<!ELEMENT hr EMPTY>
<!ELEMENT img EMPTY>
<!-- XML Usage -->
<document>
<paragraph>First line<br/>Second line</paragraph>
<hr/>
<img/>
</document>
🔹 Elements with PCDATA
PCDATA (Parsed Character Data) elements contain text that will be parsed by the XML parser. Special characters must be properly escaped.
<!-- DTD Declaration -->
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!-- XML Usage -->
<book>
<title>Learning XML</title>
<author>John Doe</author>
<price>29.99</price>
</book>
🔹 Elements with Child Elements
Elements can contain other elements as children. You can specify the order and occurrence of child elements using special operators.
🔸 Sequence (Comma)
<!-- Children must appear in exact order -->
<!ELEMENT person (firstname,lastname,age)>
<person>
<firstname>John</firstname>
<lastname>Doe</lastname>
<age>30</age>
</person>
🔸 Choice (Pipe)
<!-- Only one child can appear -->
<!ELEMENT contact (email|phone)>
<contact>
<email>[email protected]</email>
</contact>
🔹 Occurrence Indicators
Occurrence indicators control how many times an element can appear. These symbols provide flexibility in defining document structure and validation rules.
| Symbol | Meaning | Example |
|---|---|---|
| + | One or more | (item+) |
| * | Zero or more | (item*) |
| ? | Zero or one | (item?) |
| (none) | Exactly one | (item) |
<!-- One or more books required -->
<!ELEMENT library (book+)>
<!-- Zero or more comments allowed -->
<!ELEMENT article (title,content,comment*)>
<!-- Optional subtitle -->
<!ELEMENT document (title,subtitle?,content)>
<!-- Exactly one author -->
<!ELEMENT book (title,author,price)>
🔹 Mixed Content Elements
Mixed content elements can contain both text and child elements. This is useful for formatted text with inline elements like bold or italic.
<!-- DTD Declaration -->
<!ELEMENT paragraph (#PCDATA|bold|italic|underline)*>
<!ELEMENT bold (#PCDATA)>
<!ELEMENT italic (#PCDATA)>
<!ELEMENT underline (#PCDATA)>
<!-- XML Usage -->
<paragraph>
This is <bold>important</bold> text with
<italic>emphasis</italic> and <underline>underlined</underline> words.
</paragraph>
🔹 ANY Content
The ANY keyword allows an element to contain any combination of text and child elements. Use sparingly as it reduces validation strictness.
<!-- Element can contain anything -->
<!ELEMENT note ANY>
<!-- XML Usage -->
<note>
<title>My Note</title>
Some text here
<date>2024-01-15</date>
<priority>High</priority>
</note>
🔹 Complete Example
Here's a comprehensive example demonstrating different element types in a real-world document structure for a blog system.
<?xml version="1.0"?>
<!DOCTYPE blog [
<!ELEMENT blog (post+)>
<!ELEMENT post (title,author,date,content,tags?)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT content (paragraph+)>
<!ELEMENT paragraph (#PCDATA|bold|italic|link)*>
<!ELEMENT bold (#PCDATA)>
<!ELEMENT italic (#PCDATA)>
<!ELEMENT link (#PCDATA)>
<!ELEMENT tags (tag+)>
<!ELEMENT tag (#PCDATA)>
]>
<blog>
<post>
<title>Introduction to XML</title>
<author>Jane Smith</author>
<date>2024-01-15</date>
<content>
<paragraph>
XML is <bold>powerful</bold> and <italic>flexible</italic>.
</paragraph>
<paragraph>
Learn more at <link>example.com</link>.
</paragraph>
</content>
<tags>
<tag>XML</tag>
<tag>Tutorial</tag>
</tags>
</post>
</blog>