DTD Basics   «Prev  Next»

Lesson 5Element type declarations
ObjectiveDeclare Basic Elements in a DTD.

Declare Basic Elements in DTD

The syntax for declaring an element in a DTD is as follows: A DTD (Document Type Definition) in XML is used to define the structure and content of an XML document. It is a set of rules that specify the valid elements, attributes, and entities that can be used in an XML document. The basic elements of a DTD include:
  1. Document type declaration: The document type declaration defines the root element of the XML document and specifies the DTD that defines the structure and content of the document. It begins with the DOCTYPE keyword and includes the name of the root element and the name of the DTD.
  2. Element declarations: Element declarations define the elements that can be used in the XML document, including the name of the element, the content model, and the attributes that can be associated with the element. The content model specifies the structure of the element, such as whether it can have child elements or only text content.
  3. Attribute declarations: Attribute declarations define the attributes that can be associated with the elements in the XML document. They specify the name of the attribute, the data type, and any default or fixed values that are associated with the attribute.
  4. Entity declarations: Entity declarations define named entities that can be used in the XML document to represent common text strings, such as special characters or frequently used phrases. Entities can be defined as internal entities, which are defined within the DTD, or external entities, which are defined in separate files.
  5. Notation declarations: Notation declarations define the types of non-XML data that can be included in the XML document, such as images, audio files, or video files. They specify the name of the notation, the system identifier, and any additional information needed to identify and process the data.

Overall, a DTD in XML is used to define the structure and content of an XML document, including the valid elements, attributes, and entities that can be used in the document. The basic elements of a DTD include the document type declaration, element declarations, attribute declarations, entity declarations, and notation declarations.

<!ELEMENT elementName (allowed element contents)> 


Element Names

Element names must begin with a letter but cannot begin with the letters "xml" in either upper or lower case. These three letters are reserved for the specification itself. In addition, you cannot use the colon character in an element name because it is reserved for other purposes.

Declaring Elements that contain Text or other elements

The keyword ANY is used to specify that an element can contain text or other elements using the following syntax:
<!ELEMENT elementName ANY> 

Here is an example DTD element declaration that declares the CREDIT-CARD element may contain either text or other elements:
<!ELEMENT CREDIT-CARD ANY>

Declaring elements that contain only text

When an element will contain only character data, declare the element in this manner:
<!ELEMENT elementName (#PCDATA)> 

PCDATA

If only #PCDATA is specified, any text (but only text) can be included in the declared element.
This requirement means that no child elements may be present in the element within which #PCDATA is specified.

Declaring Child Elements

When one element will contain other elements, this information must be specified in the element declaration. When an element contains multiple elements, you may use the DTD syntax characters outlined in the DTD symbols table. The table below contains DTD symbols and indicates the various symbols and their meanings in the context of DTDs:


Table displaying 1) Symbol, 2) Meaning, 3) Example, 4) Description of a DTD
Table displaying 1) Symbol, 2) Meaning, 3) Example, 4) Description of a DTD

The following series of images show an example of how you specify information in the element declaration:

Specifying XML Information in the Element Declaration

1) You need 3 element declarations, one for each PERSON, FNAME, AND LNAME
1) Imagine that you need 3 element declarations, one for each PERSON, FNAME, AND LNAME. In addition, you have a few other decisions to make. For example,

2) The PERSON element has been defined to accept a single FNAME element followed by an LNAME
2) Let us say you decide the first name should always precede the last name. The element declarations for these three elements would then written like this. The PERSON element has been defined to accept a single FNAME element followed by an LNAME element, both of which must be present in a valid or conforming XML document In addition, both the FNAME and LNAME elements must contain some character data.


3) The element declaration indicates that either of the child elements FNAME or LNAME could apper.
3) The element declaration indicates that either of the child elements FNAME or LNAME could appear, and that only an FNAME or an LNAME element could follow.

4) This declaration says that one and only one FNAME and LNAME element must follow
4) This declaration says that one and only one FNAME and LNAME element must follow, but that the FNAME and LNAME could appear in either order.



Information Declaration Element

Element Content

If an element will contain only child elements and no text other than those elements, it is said to have element content. The next lesson shows you how to write element declarations for mixed content.

Defining DTD - Quiz

Click the Quiz link below to check your understanding of DTD basics.
Defining DTD - Quiz