XML DTD
1
Objectives
2
Explain Document Type Definition
Create Document Type Definitions:
–Declaring an Element
–Declaring Attributes
Explain the use of DTD
Document Type Definition (DTD)
3
It is a feature of SGML, which is inherited by XML.
It contains the list of tags that specifies the grammatical
structure of an XML document.
DTD defines the way elements relate to one another within
the document’s tree structure, and specifies the attributes.
DTD are of two types:
– An external DTD
– An internal DTD
Why Use a DTD
4
DTDs are used by XML to provide an application independent
way of sharing data.
Common DTD can be used to interchange data between
independent groups of people.
DTD can be used by the application to verify that valid data
has been entered.
It defines the legal building blocks of an XML document.
Structure of a DTD
5
DOCTYPE declaration <!DOCTYPE dtd-name [
<!ELEMENT elementname
ELEMENT declaration (element-content type) >
<!ATTLIST elementname
ATTRIBUTE declaration
attribute-name
attribute-type default-
value>
ENTITY declaration
<!ENTITY > entity-name
"entity-value">
]>
Creating Internal DTDs
6
Creating DTDs is a simple six steps process. These
steps can be listed as follows:
1. Declare all the possible elements.
2. Specify the permissible element children, if any.
3. Set the order in which elements must appear.
4. Declare all the possible element attributes.
5. Set the attribute data types and values.
6. Declare all the possible entities
Step 1: Declaring an Element
7
XML elements are declared with an element declaration.
Syntax
<!ELEMENT element-name(element-content type)>
Example
<!ELEMENT SHOWROOM (TV|LAPTOP)+>
Empty Element
8
EMPTY element-content type specifies that the element has no
child elements or character data.
Syntax
<!ELEMENT element-name EMPTY>
Example
<!ELEMENT img EMPTY>
– Empty elements with attributes are possible:
<img src="tittle.gif"/>
Elements with Data
9
Syntax
<!ELEMENT element-name (#PCDATA)>
or
<!ELEMENT element-name ANY>
Where:
#PCDATA = element contains character data that is to be parsed
ANY = element with any content
Elements with Child Elements
10
Elements with one or more children are defined with the name of the
child element inside the parentheses.
• Syntax
<!ELEMENT element-name (child-element-
name, childelement-name,.....)>
• Example
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from(#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body(#PCDATA)>
Elements with Mixed Content
11
• An element can have a mixed combination data and
children elements.
• Syntax
<!ELEMENT element-name (#PCDATA|child-
element-name| childelement-name|.....)>
• Example
<!ELEMENT note (#PCDATA|to|from|header)>
Declare Elements
12
Value Description Syntax Example
No Content These elements are empty <!ELEMENT element- <!ELEMENT img
and accept no data name EMPTY> EMPTY>
Any Content Elements can contain any <!ELEMENT element- <!ELEMENT note
combination of data name ANY> ANY>
Only Data Elements contain character <!ELEMENT element- <!ELEMENT
data name (#PCDATA)> book(#PCDATA)>
Children Elements with children are <!ELEMENT element- <!ELEMENT note
defined with the name of the name (child-element- (to, from)>
children elements inside with name, childelement- <!ELEMENT to
the same order name,.....)> (#PCDATA)>
...
Mixed Elements contain both <!ELEMENT element- <!ELEMENT note
Content character data and children name (#PCDATA|child- ((#PCDATA|to|
elements element-name| from)>
childelement-
name|.....)>
Elements Occurrences
13
Elements Occurrences
14
Declaring Attributes
15
• Elements can have attributes.
• Syntax
<!ATTLIST element-name attribute-name
attribute-type defaultvalue>
• Example <!DOCTYPE Book [
<!ELEMENT Book (Title, Chapter+)>
<!ATTLIST Book Author CDATA #REQUIRED>
<!ELEMENT Chapter (#PCDATA)>
<!ATTLIST Chapter id CDATA #REQUIRED>
<!ELEMENT Title (#PCDATA)>
]>
Attribute Value Types
16
Value Explanation
CDATA The value is character data
(eval|eval|..) The value must be an enumerated value
ID The value is an 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 predefined
Attribute Default Value
17
Value Explanation
#DEFAULT The attribute has a default value.
The attribute value must be included in the
#REQUIRED
element.
#IMPLIED The attribute does not have to be included.
#FIXED The attribute value is fixed.
Attribute Default Value
18
Syntax Example
Default value <!ATTLIST Model type CDATA
"Camera">
#IMPLIED <!ATTLIST element-name <!ATTLIST Model type CDATA
attribute-name attribute-type "Camera" #IMPLIED>
#IMPLIED>
#REQUIRED <!ATTLIST element-name <!ATTLIST Model type CDATA
attribute-name attribute-type "Camera" #REQUIRED>
#REQUIRED>
#FIXED <!ATTLIST element-name <!ATTLIST Model type CDATA
attribute-name attribute-type #FIXED "Camera" >
#FIXED “value”>
Enumerated <!ATTLIST element-name <!ATTLIST Model type
attribute-name (en1|en2|...) (Camera|Phone) "Camera">
default-value>
Internal DTD
19
• It is written directly in the XML document after the
XML declaration.
• Writing the DTD within the DOCTYPE definition is
called as Wrapping.
• The file with the DTD and XML code has a .xml
extension.
External DTD
20
• It exists outside the content of a document.
• The DTD file has a .dtd extension.
• The reference to the DTD file is added at the
beginning of the XML file.
<!DOCTYPE dtd-name SYSTEM "dtd-file-
path.dtd">
Internal Entity Declaration
21
• Entities that have their contents within the XML document
are called internal entities.
• Syntax
<!ENTITY entity-name "entity-value">
• Example
<!ENTITY writer "Nicole D.">
<!ENTITY copyright "Copyright FPT.">
In XML document the entities would be referred as shown below:
<author>&writer;©right;</author>
External Entity Declaration
22
• Entities whose contents are found outside the XML
document are called external entities.
• They are declared using the SYSTEM keyword.
• Syntax
<!ENTITY entity-name SYSTEM "URI/URL">
• Example
<!ENTITY copyright SYSTEM
"http://www.xml101.com/entities.dtd">
Q&A
23