We can call it the definition of an XML document.

By definition, I mean specifying the elements, tags, tree structure, etc. that will be used in XML.

In this way, you create a standard and validate it.

XML Building Blocks

  • Elements
  • Tags
  • Entities
  • PCDATA
  • CDATA

We will learn how to define these building blocks in a DTD.

Elements

<!ELEMENT element-name element-content>

Generally, the element definition standard is as shown above. 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 name (#PCDATA)>

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

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

If there are child elements under an element, which we call child elements, to define an element with child elements:

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

Let's expand it a bit and define an element with only one child 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 an element contains multiple instances of the same child element, we use + after the child element to indicate 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)>

For zero or more occurrences of an element, we use *:

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

For zero or one occurrence of an element, we use ?:

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

For providing choices between two or more child elements, in other words, using "or," we use |:

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

Tags

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

The tag standard is as you can see above. Example:

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

Attribute Types

TypeDescription
CDATACharacter data
(optionoption2
IDMust be a unique identifier
IDREFReference to a different element's ID
IDREFSReferences to multiple different element IDs
NMTOKENValid XML name
NMTOKENSValid XML names in a list
ENTITYEntity
ENTITIESEntities
NOTATIONNotation

Attribute Values

ValueDescription
valuedefault value
valueDefault value
#REQUIREDRequired value
#IMPLIEDOptional value
#FIXEDFixed value

We won't try all the attribute types, but showing a few examples should be sufficient. We already have an example with ID above.

Let's create an example with an attribute type of #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 Divanı">
XML:
<books>&book;</books>

To call an external entity:

<!ENTITY entity-name SYSTEM "ADDRESS">
DTD:
<!ENTITY book SYSTEM "https://mehmetcan.sahin.dev/kitap.dtd">
XML:
<books>&book;</books>

We have finished examining the DTD, now let's reinforce it with an example. Let's add the previously used library example to the DTD.

<?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 position CDATA #IMPLIED>
]>
<books>
    <book isbn="9759954949">
        <title>Yunus Emre Divanı</title>
        <author position="Compiler">Selim Yağmur</author>
        <language>Turkish</language>
        <edition>8</edition>
        <date>
            <year>2014</year>
            <month>04</month>
            <day>01</day>
        </date>
    </book>
    <book isbn="9753386203">
        <title>Risaletü'n-Nushiyye Yunus Emre</title>
        <author position="Translator">Prof. Dr. Umay Türkeş Günay</author>
        <language>Turkish</language>
        <edition>3</edition>
        <date>
            <year>2009</year>
            <month>01</month>
            <day>01</day>
        </date>
    </book>
</books>

If you want to store our DTD in a different location:

<!DOCTYPE books SYSTEM "ADDRESS">

If you wonder how to check if our DTD is valid or not:

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