DTD Attributes
Adding properties and metadata to XML elements
🏷️ DTD Attributes
Attributes provide additional information about elements. They are declared using ATTLIST and can have different types, default values, and requirements to ensure proper XML document validation.
<!-- Basic attribute syntax -->
<!ATTLIST element attribute-name type default>
<!ATTLIST book isbn CDATA #REQUIRED>
Attribute Components
Attribute Name
Identifies the attribute
<!ATTLIST book isbn ...>
Attribute Type
Defines data type allowed
<!ATTLIST book ... CDATA ...>
Default Value
Sets requirement or default
<!ATTLIST book ... #REQUIRED>
Multiple Attributes
Define several at once
<!ATTLIST book
isbn CDATA #REQUIRED
edition CDATA #IMPLIED>
🔹 Attribute Types
DTD supports various attribute types to control what values are allowed. Each type serves specific validation purposes in your XML documents.
🔸 CDATA (Character Data)
<!-- Can contain any text -->
<!ATTLIST person name CDATA #REQUIRED>
<person name="John Doe"></person>
🔸 Enumerated (List of Values)
<!-- Must be one of the listed values -->
<!ATTLIST payment method (cash|card|online) #REQUIRED>
<payment method="card"></payment>
🔸 ID (Unique Identifier)
<!-- Must be unique in document -->
<!ATTLIST student id ID #REQUIRED>
<student id="S001"></student>
<student id="S002"></student>
🔸 IDREF (Reference to ID)
<!-- Must reference an existing ID -->
<!ATTLIST enrollment student-ref IDREF #REQUIRED>
<enrollment student-ref="S001"></enrollment>
🔹 Default Value Declarations
Default declarations control whether attributes are required, optional, or have preset values. These rules ensure consistent XML document structure.
| Declaration | Meaning |
|---|---|
| #REQUIRED | Attribute must be provided |
| #IMPLIED | Attribute is optional |
| #FIXED "value" | Attribute has fixed value |
| "value" | Default value if not specified |
<!-- Required attribute -->
<!ATTLIST book isbn CDATA #REQUIRED>
<!-- Optional attribute -->
<!ATTLIST book edition CDATA #IMPLIED>
<!-- Fixed value -->
<!ATTLIST book publisher CDATA #FIXED "TechBooks">
<!-- Default value -->
<!ATTLIST book language CDATA "English">
🔹 Multiple Attributes
You can declare multiple attributes for a single element. This allows comprehensive metadata and properties for complex XML structures.
<!-- DTD Declaration -->
<!ATTLIST product
id ID #REQUIRED
name CDATA #REQUIRED
category (electronics|books|clothing) #REQUIRED
price CDATA #REQUIRED
currency CDATA "USD"
available (yes|no) "yes"
discount CDATA #IMPLIED>
<!-- XML Usage -->
<product
id="P001"
name="Laptop"
category="electronics"
price="999"
currency="USD"
available="yes"
discount="10%">
</product>
🔹 ID and IDREF Example
ID and IDREF create relationships between elements. This is useful for linking related data without nesting elements deeply.
<?xml version="1.0"?>
<!DOCTYPE school [
<!ELEMENT school (student+,course+,enrollment+)>
<!ELEMENT student (name)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT course (title)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT enrollment EMPTY>
<!ATTLIST student id ID #REQUIRED>
<!ATTLIST course id ID #REQUIRED>
<!ATTLIST enrollment
student-id IDREF #REQUIRED
course-id IDREF #REQUIRED>
]>
<school>
<student id="S001">
<name>John Doe</name>
</student>
<student id="S002">
<name>Jane Smith</name>
</student>
<course id="C001">
<title>XML Basics</title>
</course>
<course id="C002">
<title>Advanced XML</title>
</course>
<enrollment student-id="S001" course-id="C001"/>
<enrollment student-id="S001" course-id="C002"/>
<enrollment student-id="S002" course-id="C001"/>
</school>
🔹 Enumerated Attributes
Enumerated attributes restrict values to a predefined list. This ensures data consistency and prevents invalid values in your XML documents.
<!-- DTD Declaration -->
<!ATTLIST order
status (pending|processing|shipped|delivered|cancelled) #REQUIRED
priority (low|medium|high) "medium"
payment (cash|card|paypal|crypto) #REQUIRED>
<!-- XML Usage -->
<order
status="processing"
priority="high"
payment="card">
</order>
🔹 Complete Example
Here's a comprehensive example showing various attribute types working together in a library management system with proper validation.
<?xml version="1.0"?>
<!DOCTYPE library [
<!ELEMENT library (book+,member+,loan*)>
<!ELEMENT book (title,author)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT member (name,email)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT loan EMPTY>
<!ATTLIST book
id ID #REQUIRED
isbn CDATA #REQUIRED
category (fiction|nonfiction|science|history) #REQUIRED
available (yes|no) "yes"
language CDATA "English">
<!ATTLIST member
id ID #REQUIRED
type (student|faculty|public) #REQUIRED
status (active|inactive) "active">
<!ATTLIST loan
book-ref IDREF #REQUIRED
member-ref IDREF #REQUIRED
date CDATA #REQUIRED>
]>
<library>
<book id="B001" isbn="978-1234567890" category="fiction" available="no">
<title>The Great Novel</title>
<author>John Writer</author>
</book>
<book id="B002" isbn="978-0987654321" category="science">
<title>Physics Today</title>
<author>Dr. Science</author>
</book>
<member id="M001" type="student" status="active">
<name>Alice Johnson</name>
<email>[email protected]</email>
</member>
<loan book-ref="B001" member-ref="M001" date="2024-01-15"/>
</library>