0% found this document useful (0 votes)
18 views5 pages

W10 Lesson 8 - Document Type Definition - Module

The document discusses Document Type Definitions (DTDs) which define rules for XML documents. It explains what a DTD is, its syntax and purpose. The document also differentiates between external and internal DTDs. Element declarations and content specifications are described along with an example XML document that uses a DTD.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

W10 Lesson 8 - Document Type Definition - Module

The document discusses Document Type Definitions (DTDs) which define rules for XML documents. It explains what a DTD is, its syntax and purpose. The document also differentiates between external and internal DTDs. Element declarations and content specifications are described along with an example XML document that uses a DTD.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

WebApp: XML-based Web Application

1
Document Type Definition

Document Type Definition


As discussed from one of the previous lesson, an XML document to be
considered valid should be well-formed and follows a special set of rules.
These special set of rules are defined in a Document Definition. This module
focuses on one type of Document Definition called DTD (Document Type
Definition).
Upon completion of this module, you should be able to:
1. Define what is DTD (Document Type Definition) and discuss its purpose
2. Discuss the syntax of DTD (Document Type Definition)
3. Differentiate External and Internal DTD (Document Type Definition)

Document Type Definition (DTD)


XML DTD Defined
Just like what we have discussed in the previous lessons, in order to create a
valid XML document, it should be well-defined and follows a certain set of
rules such as DTD.
XML DTD is a set of rules used to describe XML language accurately. It
provides a way to constrain XML document content. It can specify what kind
of content can appear on your XML document.
Purpose of XML DTD
XML DTD is easy to write but not that powerful. The following are some of
the reasons why we use DTD:
a. It is used to make sure the XML code is in the proper format.
b. By incorporating XML DTD in XML documents, errors can be found easily.
c. With a DTD, independent groups of people can agree on a standard for
interchanging data
d. You can verify that the data you receive from the outside world is valid.
XML DTDs can specify the rules for the components of XML. This module will
focus on elements DTDs.
XML DTD is a very broad topic. For more information about XML DTDs you
can visit W3Cs website.

Document Type Definition Syntax


DTD uses a different syntax than XML. Below is the syntax of DTD.

<!DOCTYPE element DTD identifier

Course Module
[
declaration
]>

Figure 1: XML DTD Syntax


The following is a detailed explanation the DTD Syntax.
a. !DOCTYPE
Used to declare a DTD.
b. element
Used to parse the document form the specified root element
c. DTD identifier
This is an identifier for DTD. This can point to a file on the system or URL
on the internet.
d. [ ]
These are optional list of entity declarations called internal subsets.
Element Declaration
In declaring elements we follow the syntax:

<!ELEMENT Name ContentSpecifications>

Figure 2: Element Declaration Syntax


The following is a detailed explanation the Element Declaration Syntax.
a. !ELEMENT
Used to declare an element
b. Name
Name of the element you want to declare
c. ContentSpecifcation
Defines the valid content for the element
An element can contain 1 of 4 of the following content models:
a. EMPTY content
No child elements
b. ANY content
No constraint
c. Element content
Can contain specified elements
d. Mixed content
Can contain elements and character data
Specifying populations limits in DTD
a. ?
This means zero or one element
b. +
This means one or more element
c. *
This means zero or more element
Below is an example of an XML document with DTD.
WebApp: XML-based Web Application
3
Document Type Definition

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>


<!DOCTYPE letter [
<!ELEMENT letter (from,to+,message)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT message (#PCDATA)>
]>
<letter>
<to>Jenny</to>
<from>Paulo</from>
<message>Hello!</message>
</letter>

Figure 2: XML Document with DTD


In the example above, we have the lines:

<!DOCTYPE letter [
]>

This means that the root tag of our XML document should be the element/tag
<letter>.
The next line:

<!ELEMENT letter (from,to+,message)>

means that inside the tag/element <letter> the XML document is required
to have the elements/tag <from>, <to>, and, <message>. If one of these
elements is missing, then your XML will produce an error. You can also notice
the + next to the to element/tag. This means that there could be one or
more <to> element inside <letter>.
The lines:

<!ELEMENT from (#PCDATA)>


<!ELEMENT to (#PCDATA)>
<!ELEMENT message (#PCDATA)>

Refer to the body of the DTD. In these lines, the elements/tags <from>, <to>,
and, <message> have the type #PCDATA. #PCDATA means parse-able text
data.
Course Module
In order to be considered a valid XML document, the XML file should abide by
the rules set by the DTD. You can check if it is a valid XML document by using
an XML Validator.

External DTD vs. Internal DTD


External DTD
External DTDs are DTD declaration outside of an XML document. Below is an
example:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>


<!DOCTYPE letter SYSTEM "letter.dtd">
<letter>
<to>Jenny</to>
<from>Paulo</from>
<message>Hello!</message>
</letter>

As you can see in the example above, the line:

<!DOCTYPE letter SYSTEM "letter.dtd">

was added. This is a reference to an external file called letter.dtd which


contains our DTD. A DTDs extension name is .dtd.

<!ELEMENT letter (from,to+,message)>


<!ELEMENT from (#PCDATA)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT message (#PCDATA)>

Figure 3: An External DTD file


Internal DTD
Internal DTDs are DTDs included inside an XML document. Figure 2 shows
an example of Internal DTD.

Glossary
XML DTD: is a set of rules used to describe XML language accurately.

References and Supplementary Materials


Books and Journals
Goldberg, K. H. ;2010; XML Visual Quick Start Guide .2nd Edition; United
States of America; Peachpit
WebApp: XML-based Web Application
5
Document Type Definition

Press.Nguyen, V. ;2017; Using XML. United States of America; Amazon Digital


Services LLC.
Online Supplementary Reading Materials
XML DTD; https://www.tutorialspoint.com/xml/xml_dtds.htm; Accessed on
8/7/2017
DTD; http://www.xmlfiles.com/dtd/; Accessed on 8/7/2017
XML DTD; https://www.w3schools.com/xml/xml_dtd.asp; Accessed on
8/7/2017
Instructional Videos
XML Tutorial 25 - DTD Schema Elements;
https://www.youtube.com/watch?v=rGj3AGsL9uI; Accessed on 8/7/2017

Course Module

You might also like