DTD - DTD Public Identifiers and SYSTEM Identifiers

In a Document Type Definition (DTD), PUBLIC and SYSTEM identifiers are used to reference an external DTD or other external resources. They are especially useful when the DTD is stored separately from the XML document. Understanding these identifiers is important when working with reusable, standardized, or shared XML definitions.

1. What is an External DTD?

An external DTD is a DTD stored in a separate file rather than being written directly inside the XML document.

For example, suppose an XML document contains:

<?xml version="1.0"?>
<!DOCTYPE student SYSTEM "student.dtd">

<student>
    <name>Rahul</name>
    <age>21</age>
</student>

Here:

  • student.xml is the XML document.

  • student.dtd is the external DTD.

  • SYSTEM tells the XML processor where the external DTD can be found.

  • "student.dtd" specifies the location of the DTD.

The external DTD could contain:

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

The XML processor can use these declarations to validate the XML document.


2. SYSTEM Identifier

The SYSTEM identifier provides a location from which the XML processor can retrieve an external resource.

The general syntax is:

<!DOCTYPE root-element SYSTEM "location">

For example:

<!DOCTYPE student SYSTEM "student.dtd">

The location may be a relative or absolute system identifier.

A relative location:

<!DOCTYPE student SYSTEM "student.dtd">

A DTD inside a directory:

<!DOCTYPE student SYSTEM "dtd/student.dtd">

An absolute URL can also be specified:

<!DOCTYPE student SYSTEM "https://example.com/student.dtd">

The exact availability and security behavior of external resource retrieval depends on the XML parser and its configuration.

How SYSTEM works

When an XML processor encounters:

<!DOCTYPE student SYSTEM "student.dtd">

it understands that the rules for the student document are located in the external resource identified by "student.dtd".

The processor then attempts to retrieve the DTD and use its declarations while processing or validating the XML document.


3. PUBLIC Identifier

A PUBLIC identifier is designed to identify a publicly recognized or standardized external resource.

Its syntax is:

<!DOCTYPE root-element PUBLIC "public-identifier" "system-identifier">

For example:

<!DOCTYPE book PUBLIC "-//Example Organization//DTD Book 1.0//EN"
                    "book.dtd">

There are two important parts here:

PUBLIC "public-identifier" "system-identifier"

The public identifier provides a standardized name for the resource, while the system identifier provides a location from which the resource can be obtained.

The public identifier itself does not necessarily tell the processor where the file is located. Therefore, a system identifier is normally supplied as well.


4. Difference Between SYSTEM and PUBLIC

The main difference is the way the external resource is identified.

Feature SYSTEM PUBLIC
Purpose Identifies a resource by its location Identifies a resource using a public name
Location Provides a system-specific location Usually accompanied by a system location
Syntax SYSTEM "location" PUBLIC "public-id" "location"
Common use Local or application-specific DTDs Standardized or publicly recognized DTDs
Identification Based mainly on location Based on a public identifier plus location

For example, a SYSTEM declaration might be:

<!DOCTYPE library SYSTEM "library.dtd">

A PUBLIC declaration might be:

<!DOCTYPE library PUBLIC "-//Example//DTD Library 1.0//EN"
                         "library.dtd">

5. Why Use a PUBLIC Identifier?

Public identifiers are useful when a DTD represents a standardized document format that may be used by many organizations or applications.

Imagine that an industry defines a common XML format for exchanging information. Instead of every organization creating its own interpretation of the DTD, the standard can have a recognized public identifier.

For example:

<!DOCTYPE document PUBLIC "-//Example Organization//DTD Document 1.0//EN"
                          "document.dtd">

The public identifier provides a consistent way to identify the particular DTD.

This is particularly useful in environments where documents and their definitions are exchanged between different systems.


6. PUBLIC Identifier Structure

Public identifiers traditionally follow a structured format.

For example:

-//Example Organization//DTD Library 1.0//EN

This can be understood conceptually as:

-//Organization//Description//Language

The first character indicates the status of the identifier. A + generally indicates that the organization is formally registered, while - indicates that it is not formally registered.

For example:

-//Example Organization//DTD Library 1.0//EN

Here:

  • - represents the identifier status.

  • Example Organization identifies the organization.

  • DTD Library 1.0 describes the DTD.

  • EN indicates English.

The exact conventions for public identifiers come from the formal standards surrounding them, so developers should follow the identifier conventions required by the particular XML standard they are implementing.


7. PUBLIC and SYSTEM Together

A PUBLIC declaration commonly contains both identifiers:

<!DOCTYPE library PUBLIC
    "-//Example Organization//DTD Library 1.0//EN"
    "library.dtd">

The first value is the public identifier:

-//Example Organization//DTD Library 1.0//EN

The second value is the system identifier:

library.dtd

This combination provides both a logical identity and a physical location.

The public identifier describes what the external resource represents, while the system identifier gives the processor a concrete location to retrieve it from.


8. Catalogs and Public Identifiers

One important advantage of PUBLIC identifiers is that applications can use catalogs to map public identifiers to local resources.

Suppose an XML document contains:

<!DOCTYPE library PUBLIC
    "-//Example Organization//DTD Library 1.0//EN"
    "https://example.com/library.dtd">

An XML processing environment may have a catalog that maps:

-//Example Organization//DTD Library 1.0//EN

to a local DTD such as:

/usr/local/dtd/library.dtd

The processor can then use the local copy rather than necessarily retrieving the resource from the network.

This can provide advantages such as:

  • Faster processing

  • Offline operation

  • Centralized resource management

  • Reduced dependence on external servers

  • Better control over external resources

The exact catalog mechanism depends on the XML processing software being used.


9. SYSTEM Identifier with a Local File

The simplest external DTD arrangement uses a local file.

XML:

<?xml version="1.0"?>

<!DOCTYPE employee SYSTEM "employee.dtd">

<employee>
    <name>Arun</name>
    <department>IT</department>
</employee>

DTD:

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

The XML document refers to employee.dtd using the SYSTEM identifier.

If the XML processor can locate the file and external DTD processing is enabled, it can use these declarations when processing the XML document.


10. SYSTEM Identifier with a URL

A system identifier can also represent an external URI:

<!DOCTYPE employee SYSTEM "https://example.com/employee.dtd">

This tells the processor that the external resource is associated with the specified URI.

However, relying on remote DTDs can introduce practical and security problems. The server may be unavailable, the resource may change, or the XML processor may deliberately disable external resource access.

For production applications, external resource handling should therefore be carefully controlled.


11. PUBLIC Identifier Example

Consider an XML document:

<?xml version="1.0"?>

<!DOCTYPE book PUBLIC
    "-//Example Organization//DTD Book 1.0//EN"
    "book.dtd">

<book>
    <title>XML Fundamentals</title>
    <author>Ravi</author>
</book>

The DTD might contain:

<!ELEMENT book (title, author)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>

The XML processor can associate the document with the DTD through the DOCTYPE declaration.

The PUBLIC identifier:

-//Example Organization//DTD Book 1.0//EN

gives the DTD a recognized public identity, while:

book.dtd

provides a system location.


12. When to Use SYSTEM

SYSTEM identifiers are appropriate when:

  • You control the DTD.

  • The DTD belongs to a particular application.

  • The DTD is stored locally.

  • The DTD is located at a known resource location.

  • A public identifier is unnecessary.

For example:

<!DOCTYPE invoice SYSTEM "invoice.dtd">

is suitable for an application-specific invoice format.


13. When to Use PUBLIC

PUBLIC identifiers are more appropriate when:

  • The DTD represents a recognized standard.

  • Multiple applications need to identify the same DTD.

  • The document format is shared across organizations.

  • Catalog-based resource resolution is being used.

  • A formal public identity for the DTD is important.

For example:

<!DOCTYPE document PUBLIC
    "-//Example Organization//DTD Document 2.0//EN"
    "document.dtd">

14. Important Security Considerations

External DTDs can create security concerns because an XML processor may attempt to access resources outside the XML document.

For example:

<!DOCTYPE data SYSTEM "https://example.com/data.dtd">

If external resource loading is unrestricted, an application could potentially be exposed to unwanted network access or other XML-related attacks.

Therefore, modern applications commonly configure XML parsers to restrict or disable unnecessary external entity and external DTD processing.

This is especially important when XML documents originate from untrusted users or external systems.


15. Key Points to Remember

The SYSTEM identifier identifies an external resource primarily through its system location:

<!DOCTYPE student SYSTEM "student.dtd">

The PUBLIC identifier provides a public name for an external resource and is normally accompanied by a system identifier:

<!DOCTYPE student PUBLIC
    "-//Example Organization//DTD Student 1.0//EN"
    "student.dtd">

In simple application-specific XML projects, SYSTEM identifiers are generally easier and more common. PUBLIC identifiers become particularly useful when working with standardized or widely shared XML vocabularies and catalog-based resource resolution.

The most important distinction is that SYSTEM tells the processor where the external resource can be found, while PUBLIC provides a standardized public identity for that resource, typically together with a system location.