We can say it's the definition of an XML document.

By definition, I mean we specify what elements, tags, tree structures, etc. will be used in XML.

This way, you've established a standard and validated it.

XML Building Blocks

  • Elements
  • Tags
  • Entities
  • PCDATA
  • CDATA

We'll learn how to define these building blocks in DTD in order.

Elements

<!ELEMENT element-name element-content>

In general, the element definition standard is like this. To define an empty element:

<!ELEMENT element-name EMPTY>
Example:
<!ELEMENT br EMPTY>
<br />

To define a PCDATA element:

<!ELEMENT element-name (#PCDATA)>
Example:
<!ELEMENT title (#PCDATA)>

To define an element with any content, we use ANY:

<!ELEMENT element-name ANY>
Example:
<!ELEMENT book ANY>

We call it a child element if the element has elements under it. To define an element with child elements:

<!ELEMENT element-name (child1,child2...)>
Example:
<!ELEMENT book (title,author,language,edition,date)>

Let's expand a bit more; if it has only one element:

<!ELEMENT books (book)>
<!ELEMENT book (title,author,language,edition,date)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT language (#PCDATA)>
<!ELEMENT edition (#PCDATA)>
<!ELEMENT date (year,month,day)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT month (#PCDATA)>
<!ELEMENT day (#PCDATA)>

If the element has multiple same elements, we add + next to that child element to handle it.

<!ELEMENT books (book+)>
<!ELEMENT book (title,author,language,edition,date)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT language (#PCDATA)>
<!ELEMENT edition (#PCDATA)>
<!ELEMENT date (year,month,day)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT month (#PCDATA)>
<!ELEMENT day (#PCDATA)>

We use * for zero or more elements.

<!ELEMENT element-name (child*)>
Example:
<!ELEMENT books (book*)>

We use ? for zero or one element.

<!ELEMENT element-name (child?)>
Example:
<!ELEMENT books (book?)>

To offer options for two or more children, i.e., for "or" usage, we add |.

<!ELEMENT element-name (child1|child2)>
Example:
<!ELEMENT book (edition|isbn)>

Attributes

<!ATTLIST element-name attribute-name attribute-type attribute-value>

The attribute standard is as you've seen above. Example:

<!ATTLIST book isbn ID "9759954949">
XML:
<book isbn="9759954949"></book>

Attribute Types

TypeDescription
CDATAData consisting of characters
(option1option2
IDMust have a unique value
IDREFID of a different element
IDREFSIDs of different and multiple elements
NMTOKENA valid XML name
NMTOKENSA valid XML name in a list
ENTITYEntity
ENTITIESEntities
NOTATIONNotated

Attribute Values

ValueDescription
valuedefault value
#REQUIREDRequired
#IMPLIEDOptional
#FIXEDFixed value

Since we can't try all attribute types, showing a few would be sufficient. There's already an example with ID above.

Let's make an example with attribute type ID and value #REQUIRED.

<!ATTLIST element-name attribute-name ID #REQUIRED>
DTD:
<!ATTLIST book isbn ID #REQUIRED>
XML:
<book isbn="9759954949"></book>

Entities

<!ENTITY entity-name "entity-value">
DTD:
<!ENTITY book "Yunus Emre Divani">
XML:
<books>&book;</books>

To call an external entity:

<!ENTITY entity-name SYSTEM "ADDRESS">
DTD:
<!ENTITY book SYSTEM "https://example.com/book.dtd">
XML:
<books>&book;</books>

We've finished examining DTD; now let's reinforce with an example. Let's add DTD to the library example we used before.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE books [
<!ELEMENT books (book+)>
<!ELEMENT book (title,author,language,edition,date)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT language (#PCDATA)>
<!ELEMENT edition (#PCDATA)>
<!ELEMENT date (year,month,day)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT month (#PCDATA)>
<!ELEMENT day (#PCDATA)>

<!ATTLIST book isbn ID #REQUIRED>
<!ATTLIST author role CDATA #IMPLIED>
]>
<books>
    <book isbn="9759954949">
        <title>Yunus Emre Divani</title>
        <author role="Editor">Selim Yagmur</author>
        <language>Turkish</language>
        <edition>8</edition>
        <date>
            <year>2014</year>
            <month>04</month>
            <day>01</day>
        </date>
    </book>
    <book isbn="9753386203">
        <title>Risaletun-Nushiyye Yunus Emre</title>
        <author role="Translator">Prof. Dr. Umay Turkes Gunay</author>
        <language>Turkish</language>
        <edition>3</edition>
        <date>
            <year>2009</year>
            <month>01</month>
            <day>01</day>
        </date>
    </book>
</books>

If we want to store our DTD file in a different location:

<!DOCTYPE books SYSTEM "ADDRESS">

So how can we check if the DTD we wrote is valid?

You can check at http://www.w3schools.com/xml/xml_validator.asp