Dtd Attributes in XML
DTD Attributes in XML
What are DTD Attributes?
Attributes provide additional information about XML elements.
In a DTD, attributes are declared separately from elements using
<!ATTLIST>.You define which attributes an element can have, their types, and whether they are required or optional.
Syntax of Attribute Declaration in DTD
<!ATTLIST elementName attributeName attributeType defaultDeclaration>Common Attribute Types
| Attribute Type | Description | Example |
|---|---|---|
CDATA | Character data (any text) | id CDATA #REQUIRED |
ID | Unique identifier (must be unique in XML document) | id ID #REQUIRED |
IDREF | Reference to an existing ID | ref IDREF #IMPLIED |
IDREFS | Multiple references (space-separated) | refs IDREFS #IMPLIED |
ENTITY | Reference to an unparsed entity | |
ENTITIES | Multiple entity references | |
NMTOKEN | Name token (valid XML name characters) | token NMTOKEN #IMPLIED |
NMTOKENS | Multiple name tokens | |
| Enumerated values | List of possible values | `status (new |
Default Declarations
| Default Declaration | Meaning |
|---|---|
#REQUIRED | Attribute must be provided |
#IMPLIED | Attribute is optional |
#FIXED "value" | Attribute value is fixed and cannot be changed |
"defaultValue" | If attribute is missing, use this default value |
Example DTD with Attributes
<!ELEMENT book (title, author)><!ATTLIST book id ID #REQUIRED genre CDATA #IMPLIED status (available | checkedout | reserved) "available"><!ELEMENT title (#PCDATA)><!ELEMENT author (#PCDATA)>Corresponding XML Example
<book id="b001" genre="fiction" status="checkedout"> <title>1984</title> <author>George Orwell</author></book><book id="b002"> <title>Brave New World</title> <author>Aldous Huxley</author></book>The first
<book>has all attributes.The second
<book>uses default"available"forstatusand has nogenre.
Summary
| Concept | Description |
|---|---|
<!ATTLIST> | Defines attributes for an element |
| Attribute Types | CDATA, ID, IDREF, enumerations, etc. |
| Default Values | Required, optional, fixed, default |
If you want, I can help you write a full XML + DTD file with attributes and validation examples!