DTD Elements - Document Type Definition (DTD)

What is DTD Elements?

XML elements can be defined as building blocks of an XML manuscript. Elements can behave as a container to hold text, elements, attributes, media substance or mix of all.

A DTD constituent is declared with an ELEMENT declaration. When an XML file is validating by DTD, parser primarily checks for the root constituent and then the child elements are validate.

Syntax

All DTD component declaration has this general form:

  • ELEMENT declaration is used to indicate the parser that you are about to define an element.
  • Element name is the element name (also called the generic identifier) that you are defining.
  • Content defines what content (if any) can go within the element.

Element Content Types

Content of elements declaration in a DTD can be considered as below:

  • Empty content
  • Element content
  • Mixed content
  • Any content

Empty Content

This is a particular case of element declaration. This element declaration does not hold any content. These are confirmed with the keyword EMPTY.

Syntax

Following is the syntax for empty element declaration:

In the above syntax:

  • ELEMENT is the element declaration of category EMPTY
  • Element name is the name of empty element.

Example

Following is a simple instance representing empty element declaration:

In this instance address is affirmed as an empty element. The mark-up for address element would appear as <address />.

Element Content

In element declaration with element content, the content would be acceptable elements within parentheses. We can also contain more than one constituent.

Syntax

Following is syntax of element declaration with element content:

  • ELEMENT is the element declaration tag
  • Element name is the name of the element.
  • child1, child2... Are the elements and each element must have its own definition within the DTD.

Example

Below example demonstrates a simple instance for element statement with element content:

In the above instance, address is the parent element and name, company and phones no are its child elements.

List of Operators and Syntax Rules

Below table show the list of operators and syntax rules which can be functional in defining child fundamentals:

Operator Syntax Description Example
+ <!ELEMENT element-name (child1+)> It indicates that child element can occur one or more times inside parent element. <!ELEMENT address (name+)>
Child elementnamecan occur one or more times inside the element nameaddress.
* <!ELEMENT element-name (child1*)> It indicates that child element can occur zero or more times inside parent element. <!ELEMENT address (name*)>
Child elementnamecan occur zero or more times inside the element nameaddress.
? <!ELEMENT element-name (child1?)> It indicates that child element can occur zero or one time inside parent element. <!ELEMENT address (name?)>
Child elementnamecan occur zero or one time inside the element nameaddress.
, <!ELEMENT element-name (child1, child2)> It gives sequence of child elements separated by comma which must be included in the the element-name. <!ELEMENT address (name, company)>
Sequence of child elementsname,company, which must occur in the same order inside the element nameaddress.
| <!ELEMENT element-name (child1 | child2)> It allows making choices in the child element. <!ELEMENT address (name | company)>
It allows you to choose either of child elements i.e.nameorcompany, which must occur in inside the element nameaddress.

Rules

We need to follow definite rules if there is more than one element satisfied:

  • Sequences - Often the elements within DTD documents must appear in a distinct order. If this is the case, you define the content using a sequence. For example:

The declaration indicates that the <address> element must have exactly three children - <name>, <company>, and <phone> - and that they must appear in this order.

  • Choices: Suppose you need to allow one element or another, but not both. In such cases you must use the pipe (|) character. The pipe functions as an exclusive OR. For example:

Mixed Element Content

This is the grouping of (#PCDATA) and children elements. PCDATA stand for parsed character data, that is, text that is not mark-up. Within mixed substance models, text can appear by itself or it can be interspersed between elements. The rules for mixed substance models are related to the element content as discussed in the previous section.

Syntax

Following is a common syntax for mixed element content:

  • ELEMENT is the element declaration tag.
  • Element name is the name of the element.
  • PCDATA is the text that is not mark-up. #PCDATA must come first in the mixed content declaration.
  • child1, child2... are the elements and each element must have its own definition within the DTD.
  • The operator (*) must follow the mixed content declaration if children elements are included
  • The (#PCDATA) and children element declarations must be separated by the (|) operator.

Example

Following isa simple instance demonstrating the mixed content element statement in a DTD.

ANY Element Content

You can declare an element using the ANY keyword in the content. It is most frequently referred to as mixed group element. ANY is useful when you have yet to make a decision the allowable inside of the element.

Syntax

Following is the syntax for declare basics with ANY substance:

Here, the ANY keyword indicates that text (PCDATA) and/or any elements affirmed within the DTD can be used within the substance of the <element name> element. They can be used in any order any number of times. However, the ANY keyword does not allow you to contain fundamentals that are not declared within the DTD.

Example

Following is a simple instance representative the element declaration with ANY satisfied:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Document Type Definition (DTD) Topics