This document defines and provides examples of key concepts in Document Type Definitions (DTDs), including:
1. A DTD defines the structure and elements of a valid XML document. It can be internal or external.
2. A DTD is used to specify valid tags, attributes, and content in an XML document so computer programs can process it correctly.
3. The main building blocks of DTDs are elements, attributes, entities, and character data types like PCDATA and CDATA. Elements define tags, attributes provide extra information, and entities define shortcuts.
3. 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
4. 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.
5. 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.
6. 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]>
7. 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>
8. 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"
9. 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">
10. <?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:
11. 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
12. ]
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>
13. 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" />
14. 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
15. 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.
17. 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.
18. 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…
19. 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.
20. 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
21. 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)>
22. 2. Empty Elements
• Empty elements are declared with the category
keyword EMPTY:
<!ELEMENT element-name EMPTY>
Example:
<!ELEMENT br EMPTY>
XML example:
<br />
23. 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)>
24. 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>
25. 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)>
26. • 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)>
27. 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)>
28. 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+)>
29. 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*)>
30. 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?)>
31. 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))>
32. 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)*>
33. DTD - Attributes
• In a DTD, attributes are declared with an
ATTLIST declaration.
34. 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" />
35. 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
36. • 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
37. 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" />
38. #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
39. #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..
40. #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 />
41. #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
42. 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
43. 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>
44. • 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>
45. • 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
46. 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
49. 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.