Cheatography
https://cheatography.com
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 (eXtensible Markup Language) |
A flexible text format used to store and transport data, designed to be both human-readable and machine-readable |
Key features |
Platform-independent |
|
Designed for information exchange |
|
Hierarchical structure with custom tags |
1. DTD Declaration
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. |
<!ELEMENT candy_store (category_chocolate, category_gummy?)>
Entities
& |
& |
< |
< |
> |
> |
' |
' |
" |
" |
<!ENTITY copy "Copyright SweetWorld 2023">
Called in the XML document with ©
.
Parameter Entities
Reusable definitions within a DTD. |
Allows complex content to be reused in multiple elements. |
<!ENTITY % product "(price, manufacturer?, images?, description?, nutritional_info?)">
|
<!ELEMENT chocolate_bar %product;>
|
Element Occurrence
? |
Optional, zero or one occurrence |
* |
Optional, zero, one, or more occurrences |
+ |
At least one occurrence |
, |
Elements must appear in the given order |
| |
Either one element or the other |
<!ELEMENT nutritional_info (calories?, fat_content?, sugar_content?)>
Calories, fat_content, and sugar_content are optional.
|
|
What is a DTD?
DTD (Document Type Definition) |
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 Declarations
<!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
<!ATTLIST element_name attribute_name type status>
|
Status: |
#REQUIRED |
must be present |
#IMPLIED |
optional |
#FIXED |
fixed value |
Types: |
CDATA (any character string) |
|
Enumerations (predefined values) |
Example:
<!ATTLIST candy chocolate_flavor CDATA #REQUIRED sugar_content CDATA #REQUIRED>
Attributes: chocolate_flavor, sugar_content
Element: candy
|