Document Type Defination DTD

22
Document Type Definitions ARVIND PANDE Visit-arvindpandeblog.blogspot.in 1

Transcript of Document Type Defination DTD

Page 1: Document Type Defination DTD

Document Type Definitions

ARVIND PANDE

Visit-arvindpandeblog.blogspot.in

1

Page 2: Document Type Defination DTD

Why use a DTD?

XML provides an application independent way of sharing data.

It specifies the rules for validating the contents of XML document at one place. So

application programs can concentrate on processing of XML document.

DTD (Document Type Definition) to verify your own data.

Your application can use a standard DTD to verify that data that you receive from the

outside world is valid.

A DTD specifies what tags may occur, when they may occur, and what attributes they may have.

With a DTD, independent groups of people can agree to use a standard DTD for interchanging data.

2

Page 3: Document Type Defination DTD

XML and DTDs A DTD (Document Type Definition) describes the structure of one

or more XML documents.

A DTD allows to validate the content of XML document.

DTD file holds the rules related to xml document for validation purpose.

DTD describes:

Elements

Attributes, and

Entities

Xml document

contains data

DTD file contains

rules that apply to

the data

3

Page 4: Document Type Defination DTD

Type of DTD

DTD TYPES

INTERNAL DTD EXTERNAL DTD

Internal DTD :

DTD content follows the

DOCTYPE declaration.

External DTD :

It hold the reference to the DTD

file (actual DTD contents placed

in separate file.

4

Page 5: Document Type Defination DTD

Internal DTD file

<? Xml version =“1.0”?>

< ! DOCTYPE MYBOOK

[

<! ELEMENT MYBOOK (BOOKNAME,AUTHOR,PRICE)>

< ! ELEMENT BOOKNAME (#PCDATA) >

< ! ELEMENT AUTHOR (#PCDATA) >

< ! ELEMENT PRICE (#PCDATA) >

] >

<MYBOOK>

<BOOKNAME> PROGRAMMING IN C# </BOOKNAME>

<AUTHOR> BALGURU </AUTHOR>

<PRICE> 450 </PRICE>

</MYBOOK>

5

Page 6: Document Type Defination DTD

PCDATA

PCDATA means parsed character data.

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.

Tags inside the text will be treated as markup and entities will be

expanded.

parsed character data should not contain any &, <, or > characters;

these need to be represented by the &amp; &lt; and &gt; entities,

respectively.

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.

6

Page 7: Document Type Defination DTD

DTD – Elements

1. Declaring Elements

In a DTD, XML elements are declared with an element declaration

<!ELEMENT element-name category>

or

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

2. Empty Elements

Empty elements are declared with the category keyword EMPTY:

<!ELEMENT element-name EMPTY>

<!ELEMENT CHAPTER EMPTY>

7

Page 8: Document Type Defination DTD

3. Elements with Parsed Character Data

Elements with only parsed character data are declared with #PCDATA

inside parentheses:

<!ELEMENT element-name (#PCDATA)>

Example:

<!ELEMENT BOOKNAME (#PCDATA)>

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 SYLLABUS ANY>

8

Page 9: Document Type Defination DTD

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 SYLLABUS (BOOKNAME,AUTHOR,PRICE)>

6. Declaring Only One Occurrence of an Element

<!ELEMENT element-name (child-name)>

Example:

<!ELEMENT MYBOOK (BOOKNAME)>

9

Page 10: Document Type Defination DTD

7. Declaring Minimum One Occurrence of an Element

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

Example:

<!ELEMENT CHAPTER (TOPIC+)>

The + sign in the example above declares that the child element

“TOPIC“ must occur one or more times inside the “CHAPTER" element.

8. Declaring Zero or More Occurrences of an Element

<!ELEMENT element-name (child-name*)>

Example:

<!ELEMENT SYLLABUS (MSG*)>

The * sign in the example above declares that the child element “MSG" can occur zero or more times inside the “SYLLABUS" element.

10

Page 11: Document Type Defination DTD

9. Declaring Zero or One Occurrences of an Element

<!ELEMENT element-name (child-name?)>

Example:

<!ELEMENT SYLLABUS (BRANCH? )>

The ? sign in the example above declares that the child element

“BRANCH" can occur zero or one time inside the “SYLLABUS" element.

11

Page 12: Document Type Defination DTD

XML

document

Program C

Program B

Program A

DTD

validate

12

Page 13: Document Type Defination DTD

Parsers An XML parser is an API that reads the content of an XML

document

Currently popular APIs are DOM (Document Object Model) and SAX (Simple API for XML)

A validating parser is an XML parser that compares the XML

document to a DTD and reports any errors. Most browsers do not

use validating parsers

13

Page 14: Document Type Defination DTD

An XML example

<SYLLABUS><BRANCH>

<YEAR> BE </YEAR></BRANCH><CHAPTER NUMBER="1">

<TOPIC> INTRODUCTION </TOPIC><TOPIC>SERVELET </TOPIC>

</CHAPTER></SYLLABUS>

• An XML document contains (and the DTD describes)

• Elements - BRANCH and CHAPTER, consisting of tags and content

• Attributes - number="1", consisting of a name and a value

• Entities - none

14

Page 15: Document Type Defination DTD

A DTD example<!DOCTYPE SYLLABUS [

<!ELEMENT SYLLABUS (BRANCH, CHAPTER+)>

<!ELEMENT CHAPTER (TOPIC+)>

<!ELEMENT TOPIC (#PCDATA)>

<!ATTRIBUTE CHAPTER NUMBER CDATA #REQUIRED>

]>

A SYLLABUS consists of a BRANCH and one or more CHAPTERS, in that order

A CHAPTER consists of one or more TOPICS

A CHAPTER consists of parsed character data (text that cannot contain

any other elements)

Each CHAPTER must have a number attribute

15

Page 16: Document Type Defination DTD

ELEMENT descriptions

Suffixes:

? zero or one BRANCH?

+ one or more TOPIC+

* zero or more MSG*

Separators

, both, in order BOOKNAME ? , CHAPTER +

| or BOOKNAME|CHAPTER

Grouping

( ) grouping (section|chapter)+

16

Page 17: Document Type Defination DTD

Attributes and entities In addtion to elements, a DTD may declare attributes and entities

An attribute describes information that can be put within the start tag of an element

In XML: <CHAPTER NUMBER=“1" SECTION=“2”></CHAPTER>

In DTD: <!ATTLIST CHAPTERNUMBER CDATA #REQUIREDSECTIONS CDATA #IMPLIED >

An entity describes text to be substituted

In XML: &copyright;In the DTD: <!ENTITY copyright "Copyright BALGURU">

#REQUIRED : The attribute must be present

#IMPLIED: The attribute is optional

#FIXED "value“ : The attribute always has the given value

17

Page 18: Document Type Defination DTD

example: XML weatherreport

<?xml version="1.0"?>

<!DOCTYPE myXmlDoc SYSTEM "mydoc.dtd">

<weatherReport>

<date>13-07-2011</date>

<location>

<city> sangli </city>

<state> Maharashtra</state>

<country>india</country>

</location>

<temperature-range>

<high scale="F">84</high>

<low scale="F">51</low>

</temperature-range>

</weatherReport>

18

Page 19: Document Type Defination DTD

DTD file for weather report

<!ELEMENT weatherReport (date, location, temperature-range)>

<!ELEMENT date (#PCDATA)>

<!ELEMENT location (city, state, country)>

<!ELEMENT city (#PCDATA)>

<!ELEMENT state (#PCDATA)>

<!ELEMENT country (#PCDATA)>

<!ELEMENT temperature-range ((low, high)|(high, low))>

<!ELEMENT low (#PCDATA)>

<!ELEMENT high (#PCDATA)>

<!ATTLIST low scale (C|F) #REQUIRED>

<!ATTLIST high scale (C|F) #REQUIRED>

19

Page 20: Document Type Defination DTD

USE of external DTD file with Tokenised attribute.

Emp.dtd

<! ELEMENT EMP (NAME,DEPT,SALARY)>

<! ELEMENT NAME (#PCDATA) >

<! ELEMENT DEPT (#PCDATA) >

<! ELEMENT SALARY (#PCDATA) >

<! ENTITY title “PROFESSOR” >

Emp.xml

<? xml version=“1.0” ?>

<! DOCTYPE EMP SYSTEM “emp.dtd” >

<EMP>

<NAME> &title; ABCD </NAME>

<DEPT> SOFTWARE </DEPT>

<SALARY> 5000 </SALARY>

</EMP>

20

Page 21: Document Type Defination DTD

LIMITATIONS OF DTDS

Syntax of xml and

dtd are different

Syntax of writing dtd file is different than xml

One dtd per xml We can not use multiple dtd to validate one xml

document. we can include only one dtd

reference inside xml document.

No inheritance DTD are not object oriented, we can not create

our own type and can’t extend them

Overriding DTD Internal dtd override external dtd instructions.

This allows flexibility but complicated design

No DOM support DOM commands are not available for DTD.

21

Page 22: Document Type Defination DTD

THANKSFor Recent Technology , inventions ,

innovation To Download slides, programs,

tutorials , assignments and more

Visit-arvindpandeblog.blogspot.in