Attributes Entitites   «Prev  Next»

Lesson 1

How to build the DTD

In addition to declaring elements, DTDs may be used to define attributes and entities.
An attribute describes properties of the XML element in which it is included. Entities refer to data items. Two types of entities may be defined in a DTD: general entities and parameter entities. In this module we describe how to declare attributes and entities in DTDs for use in XML document instances .
  • Module Learning Objectives
    After completing this module, you will have the skills and knowledge necessary to:
    1. Determine the best way to use attributes in a DTD
    2. Declare lists of attributes
    3. Define entities in a DTD
    4. Create entities to use within a DTD
    5. Explain the concept of namespaces
    6. Explain the purpose of XML schemas


Document Type Definitions (DTDs) in XML

This is still true for XML today. Document Type Definitions (DTDs) in XML continue to be used to:
  • Declare elements: Define the structure of an XML document by specifying what elements can appear and in what order or structure.
  • Define attributes: Attributes provide additional information about elements. In DTDs, you can specify:
    • Which attributes are allowed for each element.
    • Whether they are required or optional.
    • Their types (e.g., CDATA, ID, IDREF, etc.).
  • Define entities:
    • General entities: Used within the document content to replace text or other markup. They can be:
      • Internal (defined within the DTD).
      • External (referenced from an external source).
      Useful for defining reusable pieces of text or including external content.
    • Parameter entities: Used only within the DTD itself to parameterize the DTD's content. They make it easier to maintain complex DTDs by allowing parts of the DTD to be defined once and used multiple times.

While DTDs are not as commonly used in modern XML applications due to the advent of more expressive schema languages like XML Schema (XSD), W3C XML Schema, RELAX NG, or Schematron, they are still supported and used in specific scenarios or legacy systems where their simplicity or specific features are advantageous.
Here's a simple example to illustrate:
<!DOCTYPE example [
  <!ELEMENT example (para+)>
  <!ELEMENT para (#PCDATA)>
  <!ATTLIST para type CDATA #IMPLIED>
  <!ENTITY author "John Doe">
  <!ENTITY % paraType "CDATA">
]>
<example>
  <para type="note">This is a paragraph by &author;.</para>
</example>
In this example:
  • example and para are elements.
  • type is an attribute of para.
  • &author; is a general entity that gets expanded to "John Doe" when the document is parsed.
  • %paraType; is a parameter entity used within the DTD to define the type of the type attribute in the ATTLIST declaration.

Thus, the use of DTDs for declaring elements, attributes, and defining entities remains valid in current XML practices.

XML Corporate Portals

System Identifiers

A system identifier enables you to specify the location of an external file containing DTD declarations. It is comprised of two parts: the keyword SYSTEM, and a URI reference pointing to the document's location. A URI can be a fi le on your local hard drive, a fi le on your intranet or network, or even a fi le available on the Internet:
<!DOCTYPE name SYSTEM "name.dtd" [...]>
You must type the word SYSTEM after the name of the root element in your declaration. Following the SYSTEM keyword is the URI reference to the location of the file, in quotation marks. The following examples use system identifi ers:
<!DOCTYPE name SYSTEM "file:///c:/name.dtd" [ ]>
<!DOCTYPE name SYSTEM "http://wiley.com/hr/name.dtd" [ ]>
<!DOCTYPE name SYSTEM "name.dtd">

Notice that the last example has no [and] characters. This is perfectly normal. Specifying an internal subset is optional. An XML document might conform to a DTD that uses only an internal subset, only an external subset, or both. If you do specify an internal subset, it appears between the [and], immediately following the system identifier. You will see how to use an external DTD in the next section. Take a look at an alternative way to refer to external DTDs:
The next lesson shows you how to determine the best way to use attributes in a DTD.


SEMrush Software