DTD Attribute Declaration

DTD attribute declaration: If an element have attributes, you have to declare the name of the attributes in DTD.

<!ATTLIST element_name attr_name attr_token_type attr_declaration>

Description

  • element_name specifies the element name.

  • attr_name specifies the element attribute name.

  • attr_token_type specifies the structure/character string value.

  • attr_declaration specifies the default behavior of this attributes.

DTD Attribute Declaration


Attribute Token type

Token type allow only certain value in a specific structure. following are some token type.

Character Entities
CDATA specifies the character string type data.
ID specifies the unique identifier. No element can have same ID in XML document.
IDREF specifies to a another elements id attribute value.
IDREFS specifies one or more ID reference value separated by blank spaces.
ENTITY ENTITY refer to a name of the entity. Allow only specified entity.
ENTITIES ENTITIES refer to a list of entity name separated by blank spaces.
NMTOKEN specifies the any string character exclude whitespace. Before and after whitespace automatic trimmed.
NMTOKENS specifies the multiple NMTOKEN separated by whitespace.

Attribute Declaration

Attribute declaration specifies the default behavior of the attribute.

#REQUIRED attribute must have value.

<!ATTLIST element_name attribute_name CDATA #REQUIRED>        <!-- Syntax-->
<!ATTLIST employee id CDATA #REQUIRED>                        <!-- Example-->

#IMPLIED attribute value are optional. Not compulsory to have some value.

<!ATTLIST element_name attribute_name CDATA #IMPLIED>       <!-- Syntax-->
<!ATTLIST name from CDATA #IMPLIED>                         <!-- Example-->

default_value attributes default value specifies.

<!ATTLIST element_name attribute_name CDATA "default_value">    <!-- Syntax-->
<!ATTLIST email domain CDATA "personal">                        <!-- Example-->

#FIXED defaut_value attribute must have in element and also specifies the default value.

<!ATTLIST element_name attribute_name CDATA #FIXED "value">     <!-- Syntax-->
<!ATTLIST designation discipline CDATA #FIXED "Web developer">  <!-- Example-->

Example

dtd_attr.xml
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE empinfo [
    <!ELEMENT empinfo (employee)>

    <!ELEMENT employee (name, designation, email)>
    <!ATTLIST employee id CDATA #REQUIRED>

    <!ELEMENT name (#PCDATA)>
    <!ATTLIST name from CDATA #IMPLIED>

    <!ELEMENT designation (#PCDATA)>
    <!ATTLIST designation discipline CDATA #FIXED "Web developer">

    <!ELEMENT email (#PCDATA)>
    <!ATTLIST email domain CDATA "personal">  
]>
<empinfo>
    <employee id="1">
        <name>Opal Kole</name>
        <designation>Senior Engineer</designation>
        <email>[email protected]</email>
    </employee>
    <employee id="2">
        <name from="CA">Opal Kole</name>
        <designation discipline="DBA">Senior Engineer</designation>
        <email>[email protected]</email>
    </employee>
</empinfo>

Run it...   »

Example Result