DTD - DTD ID and IDREF Relationships for Linking XML Elements

In a Document Type Definition (DTD), ID and IDREF are attribute types used to create relationships between different XML elements. They are particularly useful when one element needs to refer to another element without physically placing one element inside another. This provides a way to represent relationships, references, and connections within an XML document.

1. What is an ID in DTD?

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

For example:

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

Here, S101 uniquely identifies the student.

A DTD can define this attribute as:

<!ELEMENT student (name)>
<!ELEMENT name (#PCDATA)>

<!ATTLIST student
    id ID #REQUIRED
>

The declaration:

id ID #REQUIRED

means that:

  • id is the name of the attribute.

  • ID specifies that its value must be a unique identifier.

  • #REQUIRED means the attribute must be provided.

The XML processor checks that no two elements have the same ID value.

2. Rules for ID Values

An ID value has certain restrictions. It must be a valid XML name and must be unique within the document.

For example:

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

<student id="S102">
    <name>Priya</name>
</student>

This is valid because S101 and S102 are different.

However:

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

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

is invalid because the same ID, S101, is used more than once.

An ID should normally begin with a letter or another character permitted in an XML name. For example, S101 is appropriate, while an ID beginning with an invalid XML name character is not.

3. What is IDREF?

IDREF is used when an attribute needs to refer to an existing ID somewhere in the same XML document.

For example, suppose we have students and courses. A course enrollment can refer to a student by using the student's ID.

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

<course>
    <courseName>XML Programming</courseName>
    <studentRef>S101</studentRef>
</course>

A more direct representation using attributes could be:

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

<enrollment student="S101">
    <course>XML Programming</course>
</enrollment>

The DTD can define the relationship as:

<!ELEMENT student (name)>
<!ELEMENT name (#PCDATA)>

<!ELEMENT enrollment (course)>
<!ELEMENT course (#PCDATA)>

<!ATTLIST student
    id ID #REQUIRED
>

<!ATTLIST enrollment
    student IDREF #REQUIRED
>

Here, student in the enrollment element is an IDREF. Its value must correspond to an existing ID.

4. How ID and IDREF Work Together

The basic relationship is:

ID     → identifies an element
IDREF  → refers to that identifier

Consider:

<employee id="E101">
    <name>Anita</name>
</employee>

<manager employeeRef="E101">
    <department>Finance</department>
</manager>

The DTD could contain:

<!ELEMENT employee (name)>
<!ELEMENT name (#PCDATA)>

<!ELEMENT manager (department)>
<!ELEMENT department (#PCDATA)>

<!ATTLIST employee
    id ID #REQUIRED
>

<!ATTLIST manager
    employeeRef IDREF #REQUIRED
>

Here:

id="E101"

creates the identifier.

Then:

employeeRef="E101"

references that identifier.

The XML document therefore establishes a logical connection between the manager element and the employee element.

5. Why Use ID and IDREF?

ID and IDREF are useful when XML data contains relationships between objects or records.

For example, they can represent:

  • Employee and manager relationships

  • Student and course relationships

  • Books and authors

  • Products and categories

  • Customers and orders

  • Employees and departments

  • Documents and referenced documents

Instead of duplicating complete information, the document can store the information once and refer to it using an identifier.

For example:

<author id="A101">
    <name>Ravi Kumar</name>
</author>

<book>
    <title>XML Fundamentals</title>
    <authorRef>A101</authorRef>
</book>

The author information does not need to be repeated inside the book element.

6. IDREFS

DTD also provides IDREFS, which allows an attribute to contain references to multiple IDs.

For example:

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

<student id="S102">
    <name>Priya</name>
</student>

<student id="S103">
    <name>Anita</name>
</student>

<course enrolledStudents="S101 S102 S103">
    <name>XML Programming</name>
</course>

The DTD can define:

<!ATTLIST course
    enrolledStudents IDREFS #REQUIRED
>

Here, enrolledStudents can contain multiple ID references separated by spaces.

The relationship is therefore:

S101 → Student Rahul
S102 → Student Priya
S103 → Student Anita

The course references all three students through IDREFS.

7. IDREF vs IDREFS

The difference is straightforward:

Attribute Type Purpose
ID Defines a unique identifier
IDREF Refers to one ID
IDREFS Refers to multiple IDs

For example:

<!ATTLIST student id ID #REQUIRED>

defines an ID.

<!ATTLIST course student IDREF #REQUIRED>

allows a course to refer to one student.

<!ATTLIST course students IDREFS #REQUIRED>

allows a course to refer to multiple students.

8. IDREF Must Refer to an Existing ID

One important validation rule is that an IDREF should correspond to an existing ID.

For example:

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

<enrollment studentRef="S999">
    <course>XML</course>
</enrollment>

If there is no element with:

id="S999"

then the reference is invalid.

A valid relationship would be:

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

<enrollment studentRef="S101">
    <course>XML</course>
</enrollment>

The IDREF value S101 now corresponds to the existing ID.

9. Complete Example

Consider a company XML document:

<?xml version="1.0"?>

<!DOCTYPE company [
    <!ELEMENT company (employee+, department+)>

    <!ELEMENT employee (name)>
    <!ELEMENT name (#PCDATA)>

    <!ELEMENT department (departmentName)>
    <!ELEMENT departmentName (#PCDATA)>

    <!ATTLIST employee
        id ID #REQUIRED
        departmentRef IDREF #REQUIRED
    >

    <!ATTLIST department
        id ID #REQUIRED
    >
]>

<company>

    <employee id="E101" departmentRef="D10">
        <name>Rahul</name>
    </employee>

    <employee id="E102" departmentRef="D20">
        <name>Priya</name>
    </employee>

    <department id="D10">
        <departmentName>Finance</departmentName>
    </department>

    <department id="D20">
        <departmentName>Human Resources</departmentName>
    </department>

</company>

In this example:

E101 → Rahul
E102 → Priya

D10 → Finance
D20 → Human Resources

E101 → D10
E102 → D20

The id attribute creates unique identifiers, while departmentRef uses IDREF to establish a connection between an employee and a department.

10. Advantages of ID and IDREF

Using ID and IDREF provides several benefits.

Avoids data duplication:
Information can be defined once and referenced multiple times.

Represents relationships:
It allows XML documents to express connections between otherwise separate elements.

Improves data organization:
Large XML documents can be organized into identifiable records.

Supports validation:
A DTD processor can check whether IDs are unique and whether IDREF values correspond to identifiers.

Useful for complex XML structures:
ID and IDREF are particularly useful when the XML structure cannot conveniently represent relationships through nesting alone.

11. Important Limitations

ID and IDREF are useful, but they have limitations.

An ID must be unique throughout the XML document. You cannot normally have two elements using the same ID value.

IDREF only establishes a reference by identifier. It does not define the exact type of element being referenced.

For example:

<employee id="E101">

and:

<department id="D101">

could not both use D101 as an ID if they occur in the same XML document, because all ID values must be unique across the document.

Another limitation is that DTDs provide relatively basic relationship modeling. They do not provide the sophisticated type and structural constraints available in XML Schema.

Conclusion

DTD's ID, IDREF, and IDREFS attribute types provide a mechanism for establishing relationships between XML elements. ID uniquely identifies an element, IDREF points to one existing identifier, and IDREFS allows references to multiple identifiers. This approach is useful for representing relationships such as employees and departments, students and courses, or books and authors without duplicating information. Understanding these three attribute types is important for designing XML documents in which separate elements need to be logically connected.