What is DTD DTD defines a set of

  • Slides: 39
Download presentation
What is DTD?

What is DTD?

ü DTD defines a set of rules that describes how an XML file should

ü DTD defines a set of rules that describes how an XML file should be. ü DTD is used for structuring the XML document. SYMBOLS USED IN DTD “No symbol” means element must appear exactly once. * Means 0…infinity (element can appear any number of times). | means only one element can appear at a time from a list of elements. + means >=1(element must appear atleast once). ? means 0 (or) 1(element must appear exactly once or not at all).

HOW TO WRITE DTD FILES

HOW TO WRITE DTD FILES

STATEMENT 1: <!ELEMENT bookstore (book)> Bookstore book book

STATEMENT 1: <!ELEMENT bookstore (book)> Bookstore book book

STATEMENT 2: <!ELEMENT bookstore (book+)> Bookstore book book

STATEMENT 2: <!ELEMENT bookstore (book+)> Bookstore book book

STATEMENT 3: <!ELEMENT bookstore (book? )> Bookstore book book

STATEMENT 3: <!ELEMENT bookstore (book? )> Bookstore book book

STATEMENT 4: <!ELEMENT bookstore (book*)> Bookstore book book

STATEMENT 4: <!ELEMENT bookstore (book*)> Bookstore book book

<!ELEMENT bookstore (book | title | volume )> STATEMENT 5: Bookstore book title volume

<!ELEMENT bookstore (book | title | volume )> STATEMENT 5: Bookstore book title volume Bookstore book volume title Bookstore book title volume

STATEMENT 6: <!ELEMENT book (title, author, isbn, publisher, edition, price)> **NOTE: The order should

STATEMENT 6: <!ELEMENT book (title, author, isbn, publisher, edition, price)> **NOTE: The order should be strictly followed. book title author title isbn publisher edition price

STATEMENT 7: <!ELEMENT book (title, author, isbn, publisher, edition, price)*> book title author isbn

STATEMENT 7: <!ELEMENT book (title, author, isbn, publisher, edition, price)*> book title author isbn publisher edition price

<!ELEMENT book (name , author)> book name author book author STATEMENT 8: name author

<!ELEMENT book (name , author)> book name author book author STATEMENT 8: name author book name author

<!ELEMENT book (name , author)*> book name STATEMENT 9: name author book author name

<!ELEMENT book (name , author)*> book name STATEMENT 9: name author book author name author book name author

<!ELEMENT book (name , author)+> book name STATEMENT 10: name author book author name

<!ELEMENT book (name , author)+> book name STATEMENT 10: name author book author name author book name author

<!ELEMENT title (#PCDATA)> STATEMENT 11: title C C++ JAVA PHP

<!ELEMENT title (#PCDATA)> STATEMENT 11: title C C++ JAVA PHP

STATEMENT 12: <!ELEMENT author (#PCDATA)> author balaguruswamy Herbert Schildt author Uttam K Roy author

STATEMENT 12: <!ELEMENT author (#PCDATA)> author balaguruswamy Herbert Schildt author Uttam K Roy author Chris bates

<!ELEMENT edition (#PCDATA)> edition second edition first STATEMENT 13: edition third edition Twelfth

<!ELEMENT edition (#PCDATA)> edition second edition first STATEMENT 13: edition third edition Twelfth

STATEMENT 14: <!ELEMENT price (#PCDATA)> price $45 edition $35 edition $90 edition $55

STATEMENT 14: <!ELEMENT price (#PCDATA)> price $45 edition $35 edition $90 edition $55

TYPES OF DTD’S

TYPES OF DTD’S

ü There are two types of DTD: (i) Internal DTD: If the DTD is

ü There are two types of DTD: (i) Internal DTD: If the DTD is declared inside an XML document, then the DTD is called Internal DTD. (ii) External DTD: If the DTD is declared in a separate file, and linked to the XML document, then the DTD is called External DTD.

ü The syntax for creating Internal DTD is: <!DOCTYPE Root-Element [Element Declarations]> ü The

ü The syntax for creating Internal DTD is: <!DOCTYPE Root-Element [Element Declarations]> ü The syntax for creating External DTD is: <!DOCTYPE Root-Element SYSTEM/PUBLIC “DTD-File-Name”> ü The syntax for declaring Element in DTD is: <!ELEMENT Element-Name Type-of-Element> ü The syntax for declaring Attribute in DTD is: <!ATTLIST Default> Element-Name Attribute-Type

EXAMPLES ON INTERNAL DTD’S

EXAMPLES ON INTERNAL DTD’S

EXAMPLE 1

EXAMPLE 1

<!DOCTYPE lms [ <!ELEMENT lms (book) > Book. xml DTD (RULES) ]> <lms> <book>

<!DOCTYPE lms [ <!ELEMENT lms (book) > Book. xml DTD (RULES) ]> <lms> <book> </book> <book> </lms> XML DOCUMENT

<!DOCTYPE lms [ Book. xml <!ELEMENT lms (book) > <!ELEMENT book (name, author) >

<!DOCTYPE lms [ Book. xml <!ELEMENT lms (book) > <!ELEMENT book (name, author) > DTD (RULES) <!ELEMENT name (#PCDATA)> <!ELEMENT author (#PCDATA)> ]> <lms> <book> <name> JAVA </name> <author> Herbert Schildt </author> </book> XML DOCUMENT </lms>

<!DOCTYPE lms [ Book. xml <!ELEMENT lms (book) > <!ELEMENT book (name, author) >

<!DOCTYPE lms [ Book. xml <!ELEMENT lms (book) > <!ELEMENT book (name, author) > DTD (RULES) <!ELEMENT name (#PCDATA)> <!ELEMENT author (#PCDATA)> ]> <lms> <book> <name> JAVA </name> <author> Herbert Schildt </author> </book> </lms> XML DOCUMENT

EXAMPLE 2

EXAMPLE 2

<!DOCTYPE lms [ Book. xml <!ELEMENT lms (book*) > <!ELEMENT book (name, author) >

<!DOCTYPE lms [ Book. xml <!ELEMENT lms (book*) > <!ELEMENT book (name, author) > DTD (RULES) <!ELEMENT name (#PCDATA)> <!ELEMENT author (#PCDATA)> ]> <lms> <book> <name> JAVA </name> <author> Herbert Schildt </author> </book> <book> XML DOCUMENT <name> PHP </name> <author> Robert W Sabesta </author> </book> <name> C++ </name> <author> Balaguruswamy </author> </book> </lms>

EXAMPLE 3

EXAMPLE 3

<!DOCTYPE lms [ <!ELEMENT lms (title, author) > Book. xml DTD (RULES) ]> <lms>

<!DOCTYPE lms [ <!ELEMENT lms (title, author) > Book. xml DTD (RULES) ]> <lms> <title> <author> </title> </author> </lms> XML DOCUMENT

<!DOCTYPE lms [ <!ELEMENT lms (title, author) > <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)>

<!DOCTYPE lms [ <!ELEMENT lms (title, author) > <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> Book. xml DTD (RULES) ]> <lms> <title> JAVA </title> <author> schildt </author> XML DOCUMENT </lms>

<!DOCTYPE lms [ <!ELEMENT lms (title, author) > <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)>

<!DOCTYPE lms [ <!ELEMENT lms (title, author) > <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> Book. xml DTD (RULES) ]> <lms> <title> JAVA </title> <author> schildt </author> </lms> XML DOCUMENT

EXAMPLE 4

EXAMPLE 4

<!DOCTYPE lms [ <!ELEMENT lms (title, author)* > <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)>

<!DOCTYPE lms [ <!ELEMENT lms (title, author)* > <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> Book. xml DTD (RULES) ]> <lms> SE </title> <author> Pressman </author> <title> C++ </title> <author> Ashok </author> <title> JAVA </title> <author> Schildt </author> <title> PPL </title> <author> Sabesta </author> </lms> XML DOCUMENT

EXAMPLE 5

EXAMPLE 5

<!DOCTYPE lms [ <!ELEMENT lms (title*, author*) > Book. xml DTD (RULES) ]> <lms>

<!DOCTYPE lms [ <!ELEMENT lms (title*, author*) > Book. xml DTD (RULES) ]> <lms> <title> <author> </title> </author> </lms> XML DOCUMENT

<!DOCTYPE lms [ <!ELEMENT lms (title*, author*) > <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)>

<!DOCTYPE lms [ <!ELEMENT lms (title*, author*) > <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> Book. xml DTD (RULES) ]> <lms> <title> SE </title> JAVA </title> C++ </title> PHP </title> XML DOCUMENT <author> Pressman </author> <author> Schildt </author> <author> Ashok </author> <author> Sabesta </author> </lms>

EXAMPLES ON EXTERNAL DTD’S

EXAMPLES ON EXTERNAL DTD’S

Book. xml <? xml version=“ 1. 0” standalone=“no”? > <!DOCTYPE lms SYSTEM “book. dtd”>

Book. xml <? xml version=“ 1. 0” standalone=“no”? > <!DOCTYPE lms SYSTEM “book. dtd”> Book. dtd <!ELEMENT lms (title, author)* > <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <lms> SE </title> <author> Pressman </author> <title> C++ </title> <author> Ashok </author> <title> JAVA </title> <author> Schildt </author> <title> PPL </title> <author> Sabesta </author> </lms>