DTD Building Blocks
Core components that make up DTD structure
🧱 DTD Building Blocks
DTD consists of fundamental building blocks: elements, attributes, entities, PCDATA, and CDATA. These components work together to define complete XML document structure and validation rules.
<!-- Main building blocks -->
<!ELEMENT person (name,age)>
<!ATTLIST person id CDATA #REQUIRED>
<!ENTITY company "Tech Corp">
Main Building Blocks
Elements
Define the structure and content containers in XML documents
<!ELEMENT book (title,author)>
Attributes
Add properties and metadata to elements
<!ATTLIST book isbn CDATA #REQUIRED>
Entities
Store reusable text and special characters
<!ENTITY author "John Doe">
PCDATA
Parsed character data for text content
<!ELEMENT title (#PCDATA)>
🔹 Elements
Elements are the primary building blocks that define the structure of your XML document. They specify what content is allowed and in what order.
<!-- Empty element -->
<!ELEMENT br EMPTY>
<!-- Element with text content -->
<!ELEMENT name (#PCDATA)>
<!-- Element with child elements -->
<!ELEMENT person (firstname,lastname,age)>
<!-- Element with mixed content -->
<!ELEMENT description (#PCDATA|bold|italic)*>
Example Usage:
<person>
<firstname>John</firstname>
<lastname>Doe</lastname>
<age>30</age>
</person>
🔹 Attributes
Attributes provide additional information about elements. They are declared using ATTLIST and can have different types and default values for flexibility.
<!-- Basic attribute -->
<!ATTLIST person id CDATA #REQUIRED>
<!-- Multiple attributes -->
<!ATTLIST book
isbn CDATA #REQUIRED
edition CDATA #IMPLIED
language CDATA "English">
<!-- Enumerated attribute -->
<!ATTLIST payment method (cash|card|online) "card">
Example Usage:
<person id="P001">John Doe</person>
<book isbn="123-456" edition="2nd" language="English">
<payment method="online">
🔹 Entities
Entities are shortcuts for frequently used text or special characters. They help maintain consistency and reduce repetition throughout your XML documents.
<!-- Text entity -->
<!ENTITY company "TechCorp Industries">
<!ENTITY copyright "Copyright © 2025">
<!-- Special character entities -->
<!ENTITY nbsp " ">
<!ENTITY copy "©">
Example Usage:
<footer>
©right; &company;
</footer>
<!-- Output: Copyright © 2025 TechCorp Industries -->
🔹 PCDATA vs CDATA
Understanding the difference between PCDATA and CDATA is crucial for proper XML document structure and content handling in your applications.
PCDATA (Parsed Character Data):
- Text that will be parsed by the XML parser
- Special characters must be escaped (< > &)
- Can contain entity references
<!ELEMENT message (#PCDATA)>
<message>Price: < $100</message>
CDATA (Character Data):
- Text that will NOT be parsed
- Special characters don't need escaping
- Used for code snippets or special content
<script><![CDATA[
if (x < 10 && y > 5) {
alert("Valid");
}
]]></script>
🔹 Complete Example
Here's a comprehensive example showing all building blocks working together in a real-world scenario for a product catalog system.
<?xml version="1.0"?>
<!DOCTYPE catalog [
<!ELEMENT catalog (product+)>
<!ELEMENT product (name,description,price)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST product
id CDATA #REQUIRED
category (electronics|books|clothing) #REQUIRED
available (yes|no) "yes">
<!ENTITY currency "USD">
<!ENTITY store "TechMart">
]>
<catalog>
<product id="P001" category="electronics" available="yes">
<name>Laptop</name>
<description>High-performance laptop from &store;</description>
<price>999 ¤cy;</price>
</product>
<product id="P002" category="books">
<name>XML Guide</name>
<description>Complete XML tutorial</description>
<price>29 ¤cy;</price>
</product>
</catalog>