1. 7/9/2019
Prepared By: Dr.Saranya.K.G 1
Document Type Definitions
DTD
Prepared By: Dr.Saranya.K.G
Contents
DTD Introduction
DTD Building Blocks
DTD Elements
DTD Attributes
DTD Elements vs Attributes
DTD Entities
DTD Summary
Prepared By: Dr.Saranya.K.G
2. 7/9/2019
Prepared By: Dr.Saranya.K.G 2
What is a DTD
• Defines the structure of an XML document
• Only the elements defined in a DTD can be used in
an XML document
• can be internal or external
• A DTD defines the structure of a “valid” XML
document
• Processing overhead is incurred when validating XML
with a DTD
Prepared By: Dr.Saranya.K.G
Why Use a DTD?
• With a DTD, each of your XML files can carry a
description of its own format.
• With a DTD, independent groups of people can agree
to use a standard DTD for interchanging data.
• Your application can use a standard DTD to verify
that the data you receive from the outside world is
valid.
• You can also use a DTD to verify your own data.
Prepared By: Dr.Saranya.K.G
3. 7/9/2019
Prepared By: Dr.Saranya.K.G 3
Cont..
• XML documents are designed to be processed
by computer programs
– If you can put just any tags in an XML
document, it’s very hard to write a program
that knows how to process the tags.
– A DTD specifies what tags may occur, when
they may occur, and what attributes they
may (or must) have.
Prepared By: Dr.Saranya.K.G
DTD Declaration
• A DTD can be declared inline inside an XML
document, or as an external reference.
Internal DTD Declaration
• If the DTD is declared inside the XML file, it
should be wrapped in a DOCTYPE definition
with the following syntax:
<!DOCTYPE root-element [element-declarations]>
Prepared By: Dr.Saranya.K.G
4. 7/9/2019
Prepared By: Dr.Saranya.K.G 4
Example: XML document with an internal DTD
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
Prepared By: Dr.Saranya.K.G
The DTD above is interpreted like this:
• !DOCTYPE note defines that the root element of
this document is note
• !ELEMENT note defines that the note element
contains four elements: "to,from,heading,body"
• !ELEMENT to defines the to element to be of
type "#PCDATA"
• !ELEMENT from defines the from element to be
of type "#PCDATA"
• !ELEMENT heading defines the heading element
to be of type "#PCDATA"
• !ELEMENT body defines the body element to be
of type "#PCDATA"
Prepared By: Dr.Saranya.K.G
5. 7/9/2019
Prepared By: Dr.Saranya.K.G 5
External DTD Declaration
• If the DTD is declared in an external file, it
should be wrapped in a DOCTYPE definition
with the following syntax:
<!DOCTYPE root-element SYSTEM "filename">
Prepared By: Dr.Saranya.K.G
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
And this is the file "note.dtd" which contains the DTD:
Prepared By: Dr.Saranya.K.G
6. 7/9/2019
Prepared By: Dr.Saranya.K.G 6
DTD- XML building blocks
The building blocks of XML documents
• Seen from a DTD point of view, all XML
documents (and HTML documents) are
made up by the following building blocks:
• Elements
• Attributes
• Entities
• PCDATA
• CDATA
Prepared By: Dr.Saranya.K.G
]
Elements
• Elements are the main building blocks of both
XML and HTML documents.
• Examples of HTML elements are "body" and
"table".
• Examples of XML elements could be "note" and
"message". Elements can contain text, other
elements, or be empty. Examples of empty
HTML elements are "hr", "br" and "img".
• Examples:
<body>some text</body>
<message>some text</message>
Prepared By: Dr.Saranya.K.G
7. 7/9/2019
Prepared By: Dr.Saranya.K.G 7
Attributes
• Attributes provide extra information about
elements.
• Attributes are always placed inside the opening tag
of an element.
• Attributes always come in name/value pairs. The
following "img" element has additional information
about a source file:
• The name of the element is "img". The name of the
attribute is "src". The value of the attribute is
"computer.gif". Since the element itself is empty it is
closed by a " /".
<img src="computer.gif" />
Prepared By: Dr.Saranya.K.G
Attribute Rules
• attribute values must be placed in “ “
– in HTML this is only required id the attribute
contains the space character
• attribute values are not processed by the XML
parser
– this means the values can’t be automatically
checked by the parser
Prepared By: Dr.Saranya.K.G
8. 7/9/2019
Prepared By: Dr.Saranya.K.G 8
Entities
• Some characters have a special meaning in
XML, like the less than sign (<) that defines the
start of an XML tag.
• Most of you know the HTML entity: " ".
This "no-breaking-space" entity is used in
HTML to insert an extra space in a document.
• Entities are expanded when a document is
parsed by an XML parser.
Prepared By: Dr.Saranya.K.G
Entities
Entity References Character
< <
> >
& &
" "
' '
Prepared By: Dr.Saranya.K.G
9. 7/9/2019
Prepared By: Dr.Saranya.K.G 9
PCDATA
• PCDATA means parsed character data.
• Think of character data as the text found
between the start tag and the end tag of an
XML element.
• PCDATA is text that WILL be parsed by a
parser. The text will be examined by the parser
for entities and markup.
Prepared By: Dr.Saranya.K.G
PCDATA
• Tags inside the text will be treated as markup
and entities will be expanded.
• However, parsed character data should not
contain any &, <, or > characters; these need
to be represented by the & < and >
entities, respectively.
Cont…
Prepared By: Dr.Saranya.K.G
10. 7/9/2019
Prepared By: Dr.Saranya.K.G 10
CDATA
• CDATA means character data.
• CDATA is text that will NOT be parsed by a
parser.
• Tags inside the text will NOT be treated as
markup and entities will not be expanded.
Prepared By: Dr.Saranya.K.G
DTD - Elements
1.Declaring an Element
2.Empty Elements
3.Elements with Parsed Character Data
4.Elements with any Contents
5.Elements with Children (sequences)
6.Declaring Only One Occurrence of an Element
7.Declaring Minimum One Occurrence of an Element
8.Declaring Zero or More Occurrences of an Element
9.Declaring Zero or One Occurrences of an Element
10.Declaring either/or Content
11.Declaring Mixed Content
Prepared By: Dr.Saranya.K.G
11. 7/9/2019
Prepared By: Dr.Saranya.K.G 11
DTD - Elements
1. Declaring Elements:
• In a DTD, XML elements are declared with an
element declaration with the following syntax:
<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>
Prepared By: Dr.Saranya.K.G
2. Empty Elements
• Empty elements are declared with the category
keyword EMPTY:
<!ELEMENT element-name EMPTY>
Example:
<!ELEMENT br EMPTY>
XML example:
<br />
Prepared By: Dr.Saranya.K.G
12. 7/9/2019
Prepared By: Dr.Saranya.K.G 12
3. Elements with Data
• Elements with data are declared with the data
type inside parentheses:
<!ELEMENT element-name (#CDATA)>
Or
<!ELEMENT element-name (#PCDATA)>
Example:
<!ELEMENT note (#PCDATA)>
Prepared By: Dr.Saranya.K.G
4. Elements with any Contents
• Elements declared with the category keyword
ANY, can contain any combination of parsable
data:
<!ELEMENT element-name ANY>
Example:
<!ELEMENT note ANY>
Prepared By: Dr.Saranya.K.G
13. 7/9/2019
Prepared By: Dr.Saranya.K.G 13
5. Elements with Children (sequences)
• Elements with one or more children are declared
with the name of the children elements inside
parentheses:
<!ELEMENT element-name (child1)>
or
<!ELEMENT element-name (child1,child2,...)>
Example:
<!ELEMENT note (to,from,heading,body)>
Prepared By: Dr.Saranya.K.G
• When children are declared in a sequence
separated by commas, the children must appear
in the same sequence in the document.
• In a full declaration, the children must also be
declared, and the children can also have children.
The full declaration of the "note" element is:
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
Prepared By: Dr.Saranya.K.G
14. 7/9/2019
Prepared By: Dr.Saranya.K.G 14
6. Declaring Only One Occurrence of an
Element
• <!ELEMENT element-name (child-name)>
• The example above declares that the child
element "message" must occur once, and
only once inside the "note" element.
Example:
<!ELEMENT note (message)>
Prepared By: Dr.Saranya.K.G
7. Declaring Minimum One Occurrence of an
Element
• <!ELEMENT element-name (child-name+)>
• The + sign in the example above declares that
the child element "message" must occur one or
more times inside the "note" element.
Example:
<!ELEMENT note (message+)>
Prepared By: Dr.Saranya.K.G
15. 7/9/2019
Prepared By: Dr.Saranya.K.G 15
8. Declaring Zero or More Occurrences of an
Element
• The * sign in the example above declares that
the child element "message" can occur zero or
more times inside the "note" element.
<!ELEMENT element-name (child-name*)>
Example:
<!ELEMENT note (message*)>
Prepared By: Dr.Saranya.K.G
9. Declaring Zero or One Occurrences of an
Element
• The ? sign in the example above declares that
the child element "message" can occur zero or
one time inside the "note" element.
<!ELEMENT element-name (child-name?)>
Example:
<!ELEMENT note (message?)>
Prepared By: Dr.Saranya.K.G
16. 7/9/2019
Prepared By: Dr.Saranya.K.G 16
10. Declaring either/or Content
• The example above declares that the "note"
element must contain a "to" element, a "from"
element, a "header" element, and either a
"message" or a "body" element.
Example:
<!ELEMENT note (to,from,header,(message|body))>
Prepared By: Dr.Saranya.K.G
11. Declaring Mixed Content
• The example above declares that the "note"
element can contain zero or more occurrences
of parsed character data, "to", "from", "header",
or "message" elements.
Example:
<!ELEMENT note (#PCDATA|to|from|header|message)*>
Prepared By: Dr.Saranya.K.G
17. 7/9/2019
Prepared By: Dr.Saranya.K.G 17
DTD - Attributes
• In a DTD, attributes are declared with an
ATTLIST declaration.
Prepared By: Dr.Saranya.K.G
Declaring Attributes
An attribute declaration has the following syntax:
Declaring Attributes
An attribute declaration has the following
syntax:
<!ATTLIST element-name attribute-name attribute-type default-value>
DTD example:
<!ATTLIST payment type CDATA "check">
XML example:
<payment type="check" />
Prepared By: Dr.Saranya.K.G
18. 7/9/2019
Prepared By: Dr.Saranya.K.G 18
Type Description
CDATA The value is Character data
(en1|en2|..) The value must be one from an enumerated
list
ID The value is a unique id
IDREF The value is the id of another element
IDREFS The value is a list of other ids
NMTOKEN The value is a valid XML name
NMTOKENS The value is a list of valid XML names
ENTITY The value is an entity
ENTITIES The value is a list of entities
NOTATION The value is a name of a notation
xml: The value is a predefined xml value
Prepared By: Dr.Saranya.K.G
• The default-value can be one of the following:
Value Explanation
• value The default value of the attribute
• #REQUIRED The attribute is required
• #IMPLIED The attribute is not required
• #FIXED value The attribute value is fixed
Prepared By: Dr.Saranya.K.G
19. 7/9/2019
Prepared By: Dr.Saranya.K.G 19
A Default Attribute Value
• In the example above, the "square" element is
defined to be an empty element with a "width"
attribute of type CDATA.
• If no width is specified, it has a default value of 0.
DTD:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
Valid XML:
<square width="100" />
Prepared By: Dr.Saranya.K.G
#REQUIRED
<!ATTLIST element-name attribute-name attribute-type
#REQUIRED>
Example
DTD:
<!ATTLIST person number CDATA #REQUIRED>
Valid XML:
<person number="5677" />
Invalid XML:
<person />
Syntax
Prepared By: Dr.Saranya.K.G
20. 7/9/2019
Prepared By: Dr.Saranya.K.G 20
#REQUIRED
• Use the #REQUIRED keyword if you don't have
an option for a default value, but still want to
force the attribute to be present.
Cont..
Prepared By: Dr.Saranya.K.G
#IMPLIED
• Use the #IMPLIED keyword if you don't want to force the
author to include an attribute, and you don't have an
option for a default value.
Syntax
<!ATTLIST element-name attribute-name attribute-type #IMPLIED>
Example
DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
Valid XML:
<contact />
Prepared By: Dr.Saranya.K.G
21. 7/9/2019
Prepared By: Dr.Saranya.K.G 21
#FIXED
Syntax
<!ATTLIST element-name attribute-name
attribute-type #FIXED "value">
Example
DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML:
<sender company="Microsoft" />
Invalid XML:
<sender company="W3Schools" />
Use the #FIXED keyword when you want an attribute to have a fixed
value without allowing the author to change it. If an author includes
another value, the XML parser will return an error
Prepared By: Dr.Saranya.K.G
Enumerated Attribute Values
Syntax
<!ATTLIST element-name attribute-name (en1|en2|..)
default-value>
Example
DTD:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" />
Use enumerated attribute values when you want the attribute
value to be one of a fixed set of legal values
Prepared By: Dr.Saranya.K.G
22. 7/9/2019
Prepared By: Dr.Saranya.K.G 22
Use of Elements vs. Attributes
• The following three XML documents contain
exactly the same information:
• A date attribute is used in the first example:
<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Prepared By: Dr.Saranya.K.G
• A date element is used in the second example:
<note>
<date>12/11/2002</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Prepared By: Dr.Saranya.K.G
23. 7/9/2019
Prepared By: Dr.Saranya.K.G 23
• An expanded date element is used in the third:
<note>
<date>
<day>12</day>
<month>11</month>
<year>2002</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note
Prepared By: Dr.Saranya.K.G
DTD - Entities
• Entities are variables used to define shortcuts to
standard text or special characters.
• Entity references are references to entities
• Entities can be declared internal or external
Prepared By: Dr.Saranya.K.G
25. 7/9/2019
Prepared By: Dr.Saranya.K.G 25
DTD Summary
• You have learned how to use a DTD to define
the legal elements of an XML document, and
how a DTD can be declared inside your XML
document, or as an external reference.
• You have learned how to declare the legal
elements, attributes, entities, and CDATA
sections for XML documents.
Prepared By: Dr.Saranya.K.G