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:
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.
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.
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.
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.
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:
The following series of images show an example of how you specify information in the element declaration:
Specifying XML Information in the Element Declaration
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