DTD - ID, IDREF and IDREFS Attribute Types in DTD

In XML Document Type Definitions (DTD), ID, IDREF, and IDREFS are special attribute types used to establish relationships between different elements in an XML document. They are particularly useful when one element needs to refer to another element uniquely. Unlike ordinary attributes such as CDATA, these attribute types provide structural constraints that an XML parser can use while validating the document.

1. ID Attribute Type

The ID attribute type is used to give an element a unique identifier within an XML document. Every value assigned to an ID attribute must be unique throughout the entire document.

For example:

<!ELEMENT student EMPTY>
<!ATTLIST student
    id ID #REQUIRED
>

An XML document using this DTD could be:

<student id="S101"/>
<student id="S102"/>
<student id="S103"/>

Here, id is declared as an ID attribute. Each student must have a different ID value.

The following would be invalid:

<student id="S101"/>
<student id="S101"/>

The value S101 occurs more than once, so it violates the uniqueness requirement of the ID attribute.

An important characteristic of an ID attribute is that an XML document can have only one occurrence of a particular ID value. This makes IDs useful for identifying individual records, objects, products, employees, books, or other entities.

2. IDREF Attribute Type

The IDREF attribute type is used when an attribute needs to contain a reference to an existing ID value.

Consider this DTD:

<!ELEMENT employee EMPTY>
<!ELEMENT manager EMPTY>

<!ATTLIST employee
    id ID #REQUIRED
    manager IDREF #IMPLIED
>

<!ATTLIST manager
    id ID #REQUIRED
>

An XML document could contain:

<manager id="M101"/>
<employee id="E101" manager="M101"/>

Here, M101 is the ID of the manager. The manager attribute of the employee contains an IDREF value pointing to that ID.

The relationship can be represented conceptually as:

Employee E101
      |
      | manager
      v
Manager M101

The important point is that an IDREF value should correspond to an ID defined somewhere in the same XML document.

For example:

<employee id="E101" manager="M999"/>

would be invalid if there is no element with:

id="M999"

because the reference does not point to an existing ID.

3. IDREFS Attribute Type

Sometimes an element needs to refer to multiple elements rather than just one. This is where the IDREFS attribute type is useful.

IDREFS allows an attribute to contain a space-separated list of ID references.

For example:

<!ELEMENT student EMPTY>

<!ATTLIST student
    id ID #REQUIRED
    courses IDREFS #IMPLIED
>

An XML document could be:

<student id="S101" courses="C101 C102 C103"/>

Here, the courses attribute refers to three different IDs:

C101
C102
C103

Those IDs must be defined elsewhere in the document.

For example:

<course id="C101"/>
<course id="C102"/>
<course id="C103"/>
<student id="S101" courses="C101 C102 C103"/>

The student is therefore associated with all three courses.

Difference Between ID, IDREF and IDREFS

The three attribute types serve different purposes.

Attribute Type Purpose Number of References
ID Provides a unique identifier One unique identifier
IDREF Refers to an existing ID One ID reference
IDREFS Refers to multiple existing IDs Multiple ID references

A simple way to understand them is:

ID       → Creates an identity
IDREF    → Points to one identity
IDREFS   → Points to several identities

Complete Example

Consider an XML document describing books and authors.

The DTD could be:

<!ELEMENT library (author+, book+)>

<!ELEMENT author EMPTY>
<!ATTLIST author
    id ID #REQUIRED
    name CDATA #REQUIRED
>

<!ELEMENT book EMPTY>
<!ATTLIST book
    id ID #REQUIRED
    title CDATA #REQUIRED
    author IDREF #REQUIRED
>

An XML document could be:

<library>
    <author id="A101" name="John Smith"/>
    <author id="A102" name="Mary Jones"/>

    <book id="B101" title="XML Fundamentals" author="A101"/>
    <book id="B102" title="Web Technologies" author="A102"/>
</library>

Here:

A101 → identifies John Smith
A102 → identifies Mary Jones

B101 → identifies XML Fundamentals
B102 → identifies Web Technologies

author="A101" → refers to John Smith
author="A102" → refers to Mary Jones

The ID attributes create unique identities, while the IDREF attributes establish relationships between books and authors.

Example Using IDREFS

Suppose a book can have several authors:

<!ELEMENT book EMPTY>

<!ATTLIST book
    id ID #REQUIRED
    title CDATA #REQUIRED
    authors IDREFS #REQUIRED
>

The XML could be:

<book
    id="B101"
    title="Advanced XML"
    authors="A101 A102 A103"/>

In this case, authors contains three references. Each referenced ID must exist somewhere in the document.

For example:

<author id="A101" name="John"/>
<author id="A102" name="Mary"/>
<author id="A103" name="David"/>

<book
    id="B101"
    title="Advanced XML"
    authors="A101 A102 A103"/>

This allows the DTD-based XML structure to represent many-to-many relationships more effectively than simple text attributes.

Rules for ID Values

ID attributes have specific restrictions. An ID value must follow XML's rules for valid names, and it must be unique within the document.

For example:

id="student101"

is valid.

However:

id="101student"

is generally not a valid XML ID value because it begins with a number.

Similarly, duplicate IDs are not allowed:

<student id="S101"/>
<student id="S101"/>

The same ID cannot be assigned to two different elements.

Rules for IDREF Values

An IDREF does not create a new identifier. Instead, it references an ID that already exists in the document.

For example:

<department id="D101"/>
<employee id="E101" department="D101"/>

Here:

department="D101"

is an IDREF, while:

id="D101"

is an ID.

The reference is meaningful because D101 exists as an ID.

Rules for IDREFS Values

IDREFS contains multiple ID references separated by spaces.

Correct:

courses="C101 C102 C103"

Incorrect conceptual usage:

courses="C101,C102,C103"

The values in an IDREFS attribute are expected to form a whitespace-separated list rather than a comma-separated list.

Why ID, IDREF and IDREFS Are Useful

These attribute types are useful when XML documents contain relationships between different pieces of information.

For example, they can represent:

  • Employees and their managers

  • Students and their courses

  • Books and their authors

  • Products and their categories

  • Articles and their references

  • Departments and their employees

  • Projects and their team members

They allow relationships to be represented without physically nesting one element inside another.

For example, instead of placing an entire author element inside every book element, a book can simply contain a reference:

<book id="B101" author="A101"/>

The author can be defined separately:

<author id="A101" name="John Smith"/>

This can make large XML documents more organized and reduce duplication.

IDREF vs IDREFS

The main distinction is the number of references.

With IDREF:

manager="M101"

the attribute refers to one ID.

With IDREFS:

managers="M101 M102 M103"

the attribute can refer to several IDs.

Therefore, if an employee has only one manager, IDREF is appropriate. If an employee participates in several projects, IDREFS could be used to reference multiple project IDs.

Important Limitation

Although ID, IDREF, and IDREFS are useful, DTDs provide only relatively basic relationship constraints. They do not offer the richer data typing and relationship mechanisms available in technologies such as XML Schema.

For example, XML Schema provides more sophisticated data types and constraints than traditional DTDs.

Therefore, ID, IDREF, and IDREFS are best understood as basic identity and cross-reference mechanisms within XML documents.

Conclusion

ID, IDREF, and IDREFS are important DTD attribute types for establishing relationships between XML elements. ID assigns a unique identity to an element, IDREF allows another element to refer to one existing ID, and IDREFS allows an attribute to refer to multiple existing IDs.

The basic relationship can be remembered as:

ID
↓
Creates a unique identifier

IDREF
↓
References one ID

IDREFS
↓
References multiple IDs

Understanding these three attribute types is essential for creating structured XML documents in which elements need to be uniquely identified and connected to one another.