Top Banner
DTD D ocument T ype D efinition
27

DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

Jan 18, 2018

Download

Documents

Cameron Watson

Introduction to DTD What is DTD?  DTD stands for Document Type Definition  DTD is a schema specification method for XML documents.  DTD states what elements and attributes are used to describe content in an XML document, where each element is allowed, and which elements can appear within other elements.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD

Document Type Definition

Page 2: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

Agenda

Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A

Page 3: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

Introduction to DTD What is DTD?

DTD stands for Document Type DefinitionDTD is a schema specification method for XML

documents.DTD states what elements and attributes are

used to describe content in an XML document, where each element is allowed, and which elements can appear within other elements.

Page 4: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

Introduction to DTD Why use a DTD?

Each of your XML files can carry a description of its own format.

Independent groups of people can agree to use a standard DTD for interchanging data.

Application can use a standard DTD to verify that the data you receive from the outside world is valid.

Page 5: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

Introduction to DTD Internal DTD Declaration

A DTD which declare inside an XML document call inline DTD declaration.

It should be wrapped in a DOCTYPE definition.<!DOCTYPE root-element [

element-declarations]>

<?xml version="1.0"?> <!DOCTYPE note [

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

]> <note>

<message>Welcome to the DTD world</message> </note>

Page 6: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

Introduction to DTD External DTD Declaration

A DTD which declare in an external file (.dtd file) and call it inside an XML document.

<!DOCTYPE root-element SYSTEM "filename">

<?xml version="1.0"?>

<!DOCTYPE note SYSTEM "note.dtd">

<note> <message>Welcome to the DTD world</message>

</note>

Page 7: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Building Blocks Building blocks in an XML document

Elements Elements are the main building blocks of both XML and

HTML documents. Attributes

Attributes provide extra information about elements. Entities

Special characters in XML PCDATA

PCDATA is text that WILL be parsed by a parser. CDATA

CDATA is text that will NOT be parsed by a parser.

Page 8: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Elements Declaring Elements

In DTD, elements are declared with an ELEMENT declaration.

<!ELEMENT element-name category> or <!ELEMENT element-name (element-content)>

Empty ElementsEmpty elements are declared with the category

keyword EMPTY. <!ELEMENT element-name EMPTY>Example:

<!ELEMENT br EMPTY>XML example:

<br />

Page 9: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Elements Elements with Parsed Character Data

Elements with only parsed character data are declared with #PCDATA inside parentheses.

<!ELEMENT element-name (#PCDATA)> Example:

<!ELEMENT from (#PCDATA)>

PCDATA - Parsed Character DataPCDATA is text that WILL be parsed by a

parser. The text will be examined by the parser for

entities and markup.

Page 10: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Elements 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>

Elements with sub-elements (Children) 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)>

Page 11: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Elements Declaring Only One Occurrence of an Element

The + sign in the example bellow declares that the child element "message" must occur one or more times inside the "note" element

<!ELEMENT element-name (child-name+)>Example:

<!ELEMENT note (message+)>

Declaring Zero or More Occurrences of an Element The * sign in the example bellow 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*)>

Page 12: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Elements Declaring Zero or One Occurrences of an Element

The ? sign in the example bellow 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?)>

Declaring either/or Content The example bellow 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))>

Page 13: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Elements 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)*>

Page 14: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD ElementsDescription Syntax

Empty Elements <!ELEMENT element-name EMPTY> Elements with Parsed Character Data

<!ELEMENT element-name (#PCDATA)>

Elements with any Contents <!ELEMENT element-name ANY>

Elements with Children <!ELEMENT element-name (child1)> or <!ELEMENT element-name (child1,child2,.)>

Declaring Only One Occurrence <!ELEMENT element-name (child-name)> Declaring Minimum One Occurrence

+

Declaring Zero or More Occurrences

*

Declaring Zero or One Occurrences ?Declaring either/or Content |

Page 15: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Attributes Declaring Attributes

In a DTD, attributes are declared with an ATTLIST declaration.

<!ATTLIST element-name attribute-name attribute-type default-value>DTD example:

<!ATTLIST payment type CDATA "check"> XML example:

<payment type="check" />

Page 16: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Attributes attribute-type

Type DescriptionCDATA The value is character data(en1|en2|..) The value must be one from an enumerated listID The value is a unique idIDREF The value is the id of another elementIDREFS The value is a list of other idsNMTOKEN The value is a valid XML nameNMTOKENS The value is a list of valid XML namesENTITY The value is an entityENTITIES The value is a list of entitiesNOTATION The value is a name of a notationxml: The value is a predefined xml value

Page 17: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Attributes default-value

Value Explanationvalue The default value of the

attribute#REQUIRED The attribute is required#IMPLIED The attribute is not

required#FIXED value The attribute value is

fixed

Page 18: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Attributes A Default Attribute Value

DTD: <!ELEMENT square EMPTY> <!ATTLIST square width CDATA "0">

Valid XML: <square width="100" />

If no width is specified, it has a default value of 0.

Page 19: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Attributes #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.

DTD: <!ATTLIST person number CDATA #REQUIRED>

Valid XML: <person number="5677" />

Invalid XML: <person />

Page 20: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Attributes #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.

DTD: <!ATTLIST contact fax CDATA #IMPLIED>

Valid XML: <contact fax="555-667788" />

Valid XML: <contact />

Page 21: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Attributes #FIXED

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.

DTD: <!ATTLIST sender company CDATA #FIXED "Microsoft">

Valid XML: <sender company="Microsoft" />

Invalid XML: <sender company="W3Schools" />

Page 22: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Attributes Enumerated Attribute Values

Use enumerated attribute values when you want the attribute value to be one of a fixed set of legal values.

DTD: <!ATTLIST payment type (check|cash) "cash">

XML example: <payment type="check" /> or <payment type="cash" />

Page 23: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

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 An entity has three parts: an ampersand (&),

an entity name, and a semicolon (;).

Page 24: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Entities An Internal Entity Declaration

DTD Example:<!ENTITY writer "Donald Duck."><!ENTITY copyright "Copyright W3Schools.">

XML example:<author>&writer;&copyright;</author>

An External Entity DeclarationDTD Example:

<!ENTITY writer SYSTEM "http://www.w3schools.com/entities.dtd">

<!ENTITY copyright SYSTEM "http://www.w3schools.com/entities.dtd">

XML example:<author>&writer;&copyright;</author>

Page 25: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Exercises Write an XML document for following DTD.

Note: Consider this DTD as an external DTD.<?xml version="1.0" encoding="ISO-8859-1"?>

<!ELEMENT recipebook (recipe)+ ><!ELEMENT recipe (recipetype, title, ingredient+, step+) >

<!ELEMENT title (#PCDATA) > <!ELEMENT ingredient (#PCDATA) > <!ELEMENT step (#PCDATA) > <!ATTLIST step number CDATA #REQUIRED > <!ELEMENT recipetype EMPTY > <!ATTLIST recipetype name CDATA #REQUIRED >

Page 26: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Exercises Write a DTD for following XML document.

<?xml version="1.0"?> <!DOCTYPE PARTS SYSTEM "parts.dtd"> <PARTS>

<TITLE>Computer Parts</TITLE> <PART>

<ITEM>Motherboard</ITEM> <MANUFACTURER>ASUS</MANUFACTURER> <MODEL>P3B-F</MODEL> <COST> 123.00</COST>

</PART> <PART>

<ITEM>Video Card</ITEM> <MANUFACTURER>ATI</MANUFACTURER> <MODEL>All-in-Wonder Pro</MODEL> <COST> 160.00</COST>

</PART> </PARTS>

Page 27: DTD Document Type Definition. Agenda Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A.

DTD Q&A