Skip to content

DTD Building Blocks

XML documents are constructed from several fundamental components. Understanding these is crucial for defining document structures, whether using the older DTD (Document Type Definition) or the more modern XML Schema (XSD).

Note: While DTDs are presented here for context, XML Schema (XSD) is the recommended standard for defining XML structure in modern applications due to its greater power and flexibility.

From a structural definition viewpoint (like DTD or XSD), XML documents are primarily composed of:

  • Elements: The primary containers for data and structure.
  • Attributes: Modifiers providing extra information about elements.
  • Entities: Placeholders for special characters or reusable text.
  • PCDATA (Parsed Character Data): The textual content within elements that is processed by the parser.
  • CDATA (Character Data): Sections of text that the parser ignores, treating them as literal characters.

Elements are the workhorses of XML, defining the structure and holding the content. They consist of a start tag, content, and an end tag, or just a self-closing tag if empty.

Examples:

<note>
<to>Recipient</to>
<message>This is the content.</message>
</note>
<image source="logo.png" /> <!-- Empty element -->

Elements can contain text (PCDATA), other elements (nested structure), or be empty.

Attributes provide metadata or supplementary information about an element. They appear inside the element’s start tag as name-value pairs, with the value always quoted.

Example:

<book isbn="978-3-16-148410-0" language="en">
<title>XML Explained</title>
</book>

In this example, isbn and language are attribute names, and "978-3-16-148410-0" and "en" are their respective values. Attributes typically describe properties of the element itself, not its core content.

Entities are symbolic names that represent other content. They are primarily used for two purposes: representing special characters (like < as &lt;) and defining reusable chunks of text or external content (more common in DTDs).

When an XML parser encounters an entity reference (like &lt;), it replaces it with the content the entity represents.

Predefined Entities in XML:

Entity ReferenceCharacter
<<
>>
&&
""
''

DTDs allow defining custom entities, but this practice is less common now compared to using mechanisms within XML Schema or other processing technologies.

PCDATA refers to the text content found between the start and end tags of an element.

Crucially, this text is parsed by the XML processor. This means the parser will examine the text for any markup (like nested elements) and expand any entity references (like &amp; being turned back into &).

Example: In <message>Say &quot;Hello&quot;!</message>, the content Say &quot;Hello&quot;! is PCDATA. The parser recognizes &quot; and understands it represents a double quote character.

Directly including characters like <, &, or sometimes > within PCDATA requires using their respective entity references (&lt;, &amp;, &gt;).

CDATA sections provide a way to include blocks of text containing characters that would normally be interpreted as XML markup, without needing to escape them.

Text within a CDATA section is not parsed by the XML processor. It’s treated as literal character data.

CDATA sections start with <![CDATA[ and end with ]]>. They are useful for embedding code snippets or text containing many special characters.

Example:

<script>
<![CDATA[
function checkValue(val) {
if (val < 10 && val > 0) {
// do something
}
}
]]>
</script>

Inside the CDATA section, the < and & characters do not need to be escaped with &lt; and &amp;.