In XML DTD (Document Type Definition), attributes define additional properties or metadata associated with XML elements. They specify information that complements the element's content and structure. Here’s a simple explanation of XML DTD attributes with an example:
Attributes in XML DTD are declared using <!ATTLIST>
declarations. They describe the characteristics of elements, such as their name, data type, and default value.
Attribute Declaration (<!ATTLIST>
): This defines the attributes that can appear within an element.
Syntax:
Example
<!ATTLIST element_name attribute_name attribute_type default_value>
element_name
: The name of the element to which the attribute belongs.attribute_name
: The name of the attribute being defined.attribute_type
: Specifies the data type of the attribute's value (e.g., CDATA
, ID
, ENUMERATION
).default_value
: Optional. Specifies the default value for the attribute.2. Attribute Types:
ID
attribute value within the document.Consider an example XML DTD for defining a person element with attributes:
Example
<!DOCTYPE person [ <!ELEMENT person (name, age)> <!ELEMENT name (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ATTLIST person id ID #IMPLIED> <!ATTLIST person gender (Male|Female) "Male"> ]> <person id="123"> <name>John Doe</name> <age>30</age> </person>
<!ATTLIST person id ID #IMPLIED>
: Defines the id
attribute for the <person>
element as type ID
. ID
attributes must be unique within the XML document.<!ATTLIST person gender (Male|Female) "Male">
: Defines the gender
attribute for the <person>
element with an enumeration (Male
or Female
) and a default value of "Male"
.In summary, XML DTD attributes allow for defining additional properties and metadata associated with XML elements. They specify details such as identifiers, enumerations, and default values, enhancing the expressiveness and utility of XML documents in various applications.