DTD - Examples
DTD Declaration for a Simple XML Document:
<!DOCTYPE rootElement [
<!ELEMENT rootElement (childElement)>
<!ELEMENT childElement (#PCDATA)>
]>
<rootElement>
<childElement>This is some text.</childElement>
</rootElement>
In this example, we have a simple XML document with a root element named "rootElement" that contains a child element named "childElement" with parsed character data.
DTD Declaration with Attribute:
<!DOCTYPE rootElement [
<!ELEMENT rootElement (childElement)>
<!ELEMENT childElement (#PCDATA)>
<!ATTLIST childElement attribute CDATA #IMPLIED>
]>
<rootElement>
<childElement attribute="value">This is some text.</childElement>
</rootElement>
In this example, we have an additional attribute named "attribute" defined for the "childElement" element. The attribute has a data type of CDATA and is optional (#IMPLIED).
DTD Declaration with Enumeration:
<!DOCTYPE rootElement [
<!ELEMENT rootElement (childElement)>
<!ELEMENT childElement (#PCDATA)>
<!ATTLIST childElement status (active|inactive|pending) #REQUIRED>
]>
<rootElement>
<childElement status="active">This is some text.</childElement>
</rootElement>
In this example, the "status" attribute of the "childElement" element is restricted to three possible values: "active", "inactive", or "pending". The attribute is required (#REQUIRED).
These examples showcase the basic structure of DTD declarations, including element definitions, attribute definitions, and optional/enumerated attribute values. DTDs allow you to define the structure and constraints of XML documents, providing validation and structure guidelines for the data.
DTD Declaration with Nested Elements:
<!DOCTYPE rootElement [
<!ELEMENT rootElement (parentElement)>
<!ELEMENT parentElement (childElement+)>
<!ELEMENT childElement (#PCDATA)>
]>
<rootElement>
<parentElement>
<childElement>This is child element 1.</childElement>
<childElement>This is child element 2.</childElement>
</parentElement>
</rootElement>
In this example, the "rootElement" contains a single "parentElement" which, in turn, contains one or more "childElement" elements. Each "childElement" can contain parsed character data.
DTD Declaration with Mixed Content:
<!DOCTYPE rootElement [
<!ELEMENT rootElement (#PCDATA|childElement)*>
<!ELEMENT childElement (#PCDATA)>
]>
<rootElement>This is some text <childElement>with</childElement> mixed content.</rootElement>
In this example, the "rootElement" can contain a mixture of parsed character data (#PCDATA) and "childElement" elements. The "childElement" contains parsed character data.
DTD Declaration with External Entity:
DTD file:
<!DOCTYPE rootElement [
<!ENTITY externalContent SYSTEM "external.xml">
]>
<rootElement>
&externalContent;
</rootElement>
External file (external.xml):
<content>This is the external content.</content>
In this example, the "rootElement" references an external entity called "externalContent" defined in the DTD. The external entity retrieves its content from the "external.xml" file, which contains the desired XML content.