Print this Section

  
  

DTD Attributes

DTD Attributes

XML elements can contain attributes. An XML attribute is used to provide additional information about elements. In order to define the legal attributes that an XML element can contain, the DTD ATTLIST declaration is used.

In a DTD, attributes are declared using the following syntax:

<!ATTLIST element-name attribute-name attribute-type default value  >

Listing 2-4. DTD attribute declaration.

element-name refers to the name of the XML element, attribute-name refers to the name of the attribute, attribute-type refers to the type of attribute used to provide more information about the element, and default-value is the default value assigned to the attribute when a value is not specified. An example is shown below:

<?xml version="1.0"?>

    <Employee id="1111">
        <SSN>111-11-1111</SSN>
        <FirstName>Ann</FirstName>
        <LastName>Adams</LastName>
        <Salary>65000.00</Salary>
        <Department>Accounting</Department>
    </Employee>

Listing 2-5. DTD attribute syntax.

The XML file above includes an Employee element. The Employee element contains an attribute "id" to assign each employee a unique employee number. The DTD below is used to validate this document:

<!ELEMENT Employee (SSN,FirstName,LastName,Salary,Department)>
<!ELEMENT SSN (#PCDATA)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT LastName (#PCDATA)>
<!ELEMENT Salary (#PCDATA)>
<!ELEMENT Department(#PCDATA)>

<!ATTLIST Employee id CDATA>

Listing 2-6. DTD validation.

The ATTLIST declartion is coded below the ELEMENT declarations. Here the declaration states that the Employee element will contain an attribute named id. The attribute value will be of type CDATA (character data). No default value is specified. The most common attribute types are outlined below:

Type Description

CDATA

The value is character data. CDATA is text that will not be parsed by a parser. Tags inside the text will NOT be treated as markup and entities will not be expanded.

(item1|item2|..)

The value must be one of the items listed

ID

The value is a unique id

ENTITY

The value is an entity

The default-value can be one of the following:

Value Explanation

value

The default value of the attribute

#REQUIRED

The attribute is required. Use the #REQUIRED keyword if you don't have an option for a default value, but still want to force the attribute to be present.

#IMPLIED

The attribute is not required. Use the #IMPLIED keyword if you don't want to force the author to include an attribute, and you don't have an option for a default value.

#FIXED value

The attribute value is fixed. Use the #FIXED keyword when you want an attribute to have a fixed value without allowing the author to change it. If an author includes another value, the XML parser will return an error

The example below declares id as a required attribute for the Employee element.

<!ELEMENT Employee (SSN,FirstName,LastName,Salary,Department)>
<!ELEMENT SSN (#PCDATA)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT LastName (#PCDATA)>
<!ELEMENT Salary (#PCDATA)>
<!ELEMENT Department(#PCDATA)>

<!ATTLIST Employee id CDATA #REQUIRED>

Listing 2-7. id attribute.

The XML document containing the DTD above, must include the element Employee with an attribute id and the attribute must contain a value of type CDATA. If the XML document does not meet these requirements, it will not be considered a valid document.


TOP | NEXT: Entities