DTD
- DTD (Document Type Definition) (.dtd)
- Specifies a relatively simple
syntax for a set of XML files (validation criteria)
- Can't
specify complex relationships or data formats
- Does not support XML namespaces
- The DTD is
non-heirarchical (flat, linear)
- DTD is not an XML language (which makes
it more difficult to
parse or transform)
? Optional (zero or one)
* Zero or more
+ One or more
#PCDATA Parsed character data
#CDATA Unparsed character data
( #PCDATA [| element]* )* Mixed-content mode; any number of elements
can be interspersed with text
#REQUIRED The attribute value must be specified in the document.
#IMPLIED The value need not be specified in the document. If it isn't,
the application will have a default value it uses.
"defaultValue" The default value to use, if a value is not specified
in the document.
#FIXED "fixedValue" The value to use. If the document specifies any value
at all, it must be the same.
Examples
<!-- first line of .dtd file -->
<?xml version="1.0" encoding="UTF-8"?>
<!-- slideshow element contains only 1 or more slide elements -->
<!ELEMENT slideshow (slide+)>
<!-- picture element contains only 1 or more pairs of the specified items -->
<!ELEMENT picture ((image, title)+)>
<!-- slide element contains only a title followed by zero or more item elements -->
<!ELEMENT slide (title, item*)>
<!-- title element consists entirely of parsed character data (PCDATA) -->
<!ELEMENT title (#PCDATA)>
<!-- item element contains only zero or more occurrences of PCDATA or item elements
(interspersed) (mixed-content model) -->
<!ELEMENT item (#PCDATA | item)* >
<!-- slide element's type attribute must be given as
type="tech", type="exec", or type="all" -->
<!ATTLIST slide
type (tech | exec | all) #IMPLIED
>
Sample DTD files
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD for a cd catalog -->
<!ELEMENT catalog (cd*)>
<!ELEMENT cd (title, artist)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT artist (#PCDATA) >
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD for a simple "slide show" -->
<!ELEMENT slideshow (slide+)>
<!ATTLIST slideshow
title CDATA #REQUIRED
date CDATA #IMPLIED
author CDATA "unknown"
>
<!ELEMENT slide (title, item*)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT item (#PCDATA | item)* >
<!ENTITY ldquo "“"> <!-- Left Double Quote -->
<!ENTITY rdquo "”"> <!-- Right Double Quote -->
<!ENTITY trade "™"> <!-- Trademark Symbol (TM) -->
<!ENTITY rtrade "®"> <!-- Registered Trademark (R) -->
<!ENTITY copyr "©"> <!-- Copyright Symbol -->
Sample usage in XML file
- After the XML declaration and before the
root
element
Internal DTD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog [
<!ELEMENT catalog (cd*)>
<!ELEMENT cd (title, artist)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT artist (#PCDATA)>
]>
<catalog>
<cd>
<title>...</title>
<artist>...</artist>
</cd>
...
</catalog>
External DTD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "filename.dtd">
<catalog>
<cd>
<title>...</title>
<artist>...</artist>
</cd>
...
</catalog>