DTD - DTD Attribute Types: CDATA, ID, IDREF, IDREFS, NMTOKEN, and NMTOKENS
In XML, attributes provide additional information about elements. For example, in <student id="S101" name="Rahul">, id and name are attributes of the student element. A Document Type Definition (DTD) allows us to specify not only which attributes are permitted, but also what kind of values those attributes can contain. This helps an XML parser validate whether attribute values follow the rules defined by the DTD. XML defines several attribute types, including CDATA, ID, IDREF, IDREFS, NMTOKEN, and NMTOKENS. (W3C)
1. CDATA
CDATA stands for Character Data. It is the most general and commonly used DTD attribute type. A CDATA attribute can contain ordinary text as its value, provided the value is valid as an XML attribute value.
For example:
<!ELEMENT student EMPTY>
<!ATTLIST student
name CDATA #REQUIRED
city CDATA #IMPLIED
>
A valid XML document could be:
<student name="Rahul" city="Bangalore"/>
Here, both name and city are declared as CDATA. The DTD does not require name or city to follow a special pattern such as being a number or a unique identifier.
Another example is:
<!ATTLIST product
description CDATA #REQUIRED
>
The following values can be used:
<product description="Wireless Keyboard"/>
<product description="High-quality office chair"/>
<product description="Product available in multiple sizes"/>
CDATA should therefore be used when the attribute contains general textual information and no stronger DTD restriction is required. (W3C)
2. ID
The ID type is used when an attribute needs to provide a unique identifier for an element.
For example:
<!ELEMENT student EMPTY>
<!ATTLIST student
id ID #REQUIRED
>
The XML document might contain:
<student id="S101"/>
<student id="S102"/>
<student id="S103"/>
Each id value must be unique within the XML document. Therefore, this would be invalid:
<student id="S101"/>
<student id="S101"/>
The reason is that an ID value must uniquely identify an element in the document. XML also requires an ID value to conform to the XML Name rules. (W3C)
A common use of ID is to give every record a unique identifier.
For example:
<employee id="E101" name="Anita"/>
<employee id="E102" name="Ravi"/>
<employee id="E103" name="Kiran"/>
The id attribute allows individual elements to be distinguished from one another.
One important rule is that an element type cannot have more than one ID attribute. Also, an ID attribute must have #REQUIRED or #IMPLIED as its declared default rather than an ordinary default value. (W3C)
3. IDREF
IDREF means ID Reference. It is used when an attribute needs to refer to the ID value of another element in the same XML document.
Consider this DTD:
<!ELEMENT student EMPTY>
<!ELEMENT teacher EMPTY>
<!ATTLIST student
id ID #REQUIRED
teacher IDREF #REQUIRED
>
<!ATTLIST teacher
id ID #REQUIRED
>
The XML can be:
<teacher id="T101"/>
<student id="S101" teacher="T101"/>
Here:
teacher="T101"
is an IDREF.
The value T101 refers to the element whose ID is:
id="T101"
The important point is that an IDREF must correspond to an existing ID value somewhere in the XML document. Therefore, the following is invalid if no element has id="T999":
<student id="S101" teacher="T999"/>
This makes IDREF useful for creating relationships between XML elements. (W3C)
For example, it can represent relationships such as:
Student → Teacher
Employee → Department
Book → Author
Order → Customer
4. IDREFS
IDREFS is the plural form of IDREF. It allows an attribute to contain multiple references to ID values. The references are separated by whitespace.
For example:
<!ELEMENT course EMPTY>
<!ELEMENT student EMPTY>
<!ATTLIST student
id ID #REQUIRED
>
<!ATTLIST course
students IDREFS #REQUIRED
>
The XML could be:
<student id="S101"/>
<student id="S102"/>
<student id="S103"/>
<course students="S101 S102 S103"/>
Here, the students attribute contains three references:
S101
S102
S103
Each of these values must correspond to an existing ID.
For example:
<course students="S101 S102 S999"/>
would be invalid if S999 does not exist as an ID.
IDREFS is useful when one element needs to establish relationships with multiple other elements. (W3C)
5. NMTOKEN
NMTOKEN stands for Name Token. It restricts an attribute value to a valid XML name token.
For example:
<!ELEMENT product EMPTY>
<!ATTLIST product
code NMTOKEN #REQUIRED
>
Valid examples include:
<product code="P101"/>
<product code="product-101"/>
<product code="ABC_123"/>
An NMTOKEN is different from an ordinary CDATA value because it must follow the XML rules for a name token. It is not intended for arbitrary sentences or unrestricted text. The XML specification defines Nmtoken as one or more allowed name characters. (W3C)
For example, an attribute such as:
description="Wireless Keyboard"
would generally be better represented using CDATA, because it contains ordinary text with a space.
An NMTOKEN is more appropriate for values such as:
product-101
A123
version_2
6. NMTOKENS
NMTOKENS allows multiple NMTOKEN values separated by whitespace.
For example:
<!ELEMENT product EMPTY>
<!ATTLIST product
keywords NMTOKENS #REQUIRED
>
A valid XML element could be:
<product keywords="computer keyboard wireless"/>
The attribute contains three name tokens:
computer
keyboard
wireless
Another example is:
<product keywords="electronics office equipment"/>
Each individual value must satisfy the rules for an NMTOKEN, and multiple values are separated by whitespace. (W3C)
NMTOKENS is therefore useful when an attribute needs to contain a list of token-like values rather than unrestricted text.
Difference Between NMTOKEN and NMTOKENS
The main difference is the number of values allowed.
<!ATTLIST product
category NMTOKEN #REQUIRED
keywords NMTOKENS #REQUIRED
>
A possible XML document is:
<product
category="electronics"
keywords="computer keyboard wireless"/>
Here:
category = electronics
contains one NMTOKEN.
Whereas:
keywords = computer keyboard wireless
contains multiple NMTOKEN values and therefore uses NMTOKENS.
Difference Between ID, IDREF, and IDREFS
These three types are closely related but have different purposes.
| Type | Purpose | Number of values | Must correspond to an ID? |
|---|---|---|---|
ID |
Uniquely identifies an element | One | No |
IDREF |
References another element | One | Yes |
IDREFS |
References multiple elements | Multiple | Yes, each reference |
For example:
<teacher id="T101"/>
<student id="S101" teacher="T101"/>
The first id creates the identifier:
T101
The teacher attribute then refers to it:
teacher="T101"
For multiple references:
<course students="S101 S102 S103"/>
IDREFS can reference all three student IDs.
Difference Between CDATA and NMTOKEN
The distinction between CDATA and NMTOKEN is also important.
Consider:
<!ATTLIST product
description CDATA #REQUIRED
code NMTOKEN #REQUIRED
>
The following is appropriate:
<product
description="Wireless Keyboard for Office Use"
code="KB-101"/>
The description contains normal text, so CDATA is appropriate.
The code is a token-like value, so NMTOKEN is appropriate.
In simple terms:
CDATA → general text
NMTOKEN → one XML name token
NMTOKENS → multiple XML name tokens
Complete Example
The following example demonstrates several attribute types together:
<!DOCTYPE university [
<!ELEMENT university (teacher*, student*, course*)>
<!ELEMENT teacher EMPTY>
<!ELEMENT student EMPTY>
<!ELEMENT course EMPTY>
<!ATTLIST teacher
id ID #REQUIRED
name CDATA #REQUIRED
>
<!ATTLIST student
id ID #REQUIRED
name CDATA #REQUIRED
teacher IDREF #REQUIRED
>
<!ATTLIST course
code NMTOKEN #REQUIRED
students IDREFS #IMPLIED
keywords NMTOKENS #IMPLIED
>
]>
A corresponding XML document could be:
<university>
<teacher
id="T101"
name="Dr. Kumar"/>
<student
id="S101"
name="Rahul"
teacher="T101"/>
<student
id="S102"
name="Anita"
teacher="T101"/>
<course
code="XML101"
students="S101 S102"
keywords="XML DTD validation"/>
</university>
In this example:
-
teacher id="T101"usesIDto uniquely identify the teacher. -
teacher name="Dr. Kumar"usesCDATAfor ordinary text. -
student id="S101"andstudent id="S102"useIDto uniquely identify students. -
student teacher="T101"usesIDREFto connect each student with the teacher. -
course students="S101 S102"usesIDREFSto refer to multiple students. -
course code="XML101"usesNMTOKENfor a single token. -
course keywords="XML DTD validation"usesNMTOKENSfor multiple tokens.
These attribute types allow a DTD to impose meaningful restrictions on XML attributes instead of treating every attribute value as unrestricted text. The XML specification formally classifies these as string and tokenized attribute types and defines the validation rules for each. (W3C)
Advantages of Using Specific DTD Attribute Types
Using the correct attribute type improves the quality and consistency of XML documents.
1. Better validation: The XML parser can identify invalid attribute values according to the DTD rules.
2. Unique identification: ID prevents duplicate identifiers within the document.
3. Relationship management: IDREF and IDREFS allow elements to reference other elements.
4. Controlled token values: NMTOKEN and NMTOKENS restrict values to XML name-token formats.
5. Flexible text storage: CDATA is suitable when the attribute needs to contain general textual information.
6. Better document structure: Choosing appropriate types makes the intended meaning of attributes clearer to developers and XML-processing applications.
Summary
DTD attribute types determine what kind of values an XML attribute can contain. CDATA is used for general character data, while ID provides a unique identifier for an element. IDREF references one existing ID, and IDREFS references multiple existing IDs. NMTOKEN permits one XML name token, whereas NMTOKENS permits multiple name tokens separated by whitespace. These types are important because they allow DTD validation to enforce meaningful rules on XML attributes rather than simply accepting arbitrary text. (W3C)
For practical DTD design, the key distinction to remember is:
CDATA → General text
ID → Unique identifier
IDREF → Reference to one ID
IDREFS → References to multiple IDs
NMTOKEN → One name token
NMTOKENS → Multiple name tokens