Show Menu
Cheatography

DTDs for XML Cheat Sheet (DRAFT) by

Short summary on how to use a DTD with XML

This is a draft cheat sheet. It is a work in progress and is not finished yet.

What is XML?

XML
(eXten­sible Markup Language)
A flexible text format used to store and transport data, designed to be both
human-­rea­dable and machin­e-r­eadable
Key features
Platfo­rm-­ind­epe­ndent
 
Designed for inform­ation exchange
 
Hierar­chical structure with custom tags

1. DTD Declar­ation

External DTD file:
<!DOCTYPE root_element SYSTEM "file.dtd">

Internal DTD file:
<!DOCTYPE root_element [
  <!-- DTD declarations go here -->
]>
SYSTEM: Points to an external DTD file.

Root Element & Hierarchy

Every XML document has one root element that holds the entire structure.
<!E­LEMENT candy_­store (categ­ory­_ch­oco­late, catego­ry_­gum­my?­)>

Entities

&amp;
&
&lt;
<
&gt;
>
&apos;
'
&quot;
"
<!E­NTITY copy "­Cop­yright SweetWorld 2023">


Called in the XML document with
&copy;
.

Parameter Entities

Reusable defini­tions within a DTD.
Allows complex content to be reused in multiple elements.
<!E­NTITY % product "­(price, manufa­ctu­rer?, images?,
descri­ption?, nutrit­ion­al_­inf­o?)­">
<!E­LEMENT chocol­ate_bar %produ­ct;>

Element Occurrence

?
Optional, zero or one occurrence
*
Optional, zero, one, or more occurr­ences
+
At least one occurrence
,
Elements must appear in the given order
|
Either one element or the other
<!E­LEMENT nutrit­ion­al_info (calor­ies?, fat_content?,
sugar_­con­ten­t?)>

Calories, fat_co­ntent, and sugar_­content are optional.
 

What is a DTD?

DTD
(Document Type
Defini­tion)
Defines the structure and rules for an XML document.
It ensures that the XML follows a specific schema.
Key features
Elements
 
Attributes
 
Entities

2. Element Declar­ations

<!ELEMENT element_name (content)>
<!ELEMENT element_name EMPTY>

Example with sub-elements:
<!ELEMENT candy_types (chocolate, gummy, hard_candy)>

Example with text content:
<!ELEMENT description (#PCDATA)>
EMPTY: If the element has no content
PCDATA: Parsed Character Data is text that will be parsed by the XML parser. Tags inside are treated as markup and entities will be expanded.
CDATA: (Unparsed) Character data is text which is not parsed further in an XML document.
Tags inside are not treated as markup and entities will not be expanded.

Attributes

<!A­TTLIST elemen­t_name attrib­ute­_name type status>
Status:
#REQUIRED
must be present
#IMPLIED
optional
#FIXED
fixed value
Types:
CDATA (any character string)
 
Enumer­ations (prede­fined values)
Example:
<!A­TTLIST candy chocol­ate­_flavor CDATA #REQUIRED sugar_content
CDATA #REQUI­RED>


Attrib­utes: chocol­ate­_fl­avor, sugar_­content
Element: candy