Download
document type definition dtd n.
Skip this Video
Loading SlideShow in 5 Seconds..
Document Type Definition (DTD) PowerPoint Presentation
Download Presentation
Document Type Definition (DTD)

Document Type Definition (DTD)

302 Views Download Presentation
Download Presentation

Document Type Definition (DTD)

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Document Type Definition (DTD) Outline6.1 Introduction6.2 Parsers, Well-formed and Valid XML Documents6.3 Document Type Declaration6.4 Element Type Declarations 6.4.1 Sequences, Pipe Characters and Occurrence Indicators 6.4.2 EMPTY, Mixed Content and ANY6.5 Attribute Declarations 6.5.1 Attribute Defaults (#REQUIRED, #IMPLIED, #FIXED)6.6 Attribute Types 6.6.1 Tokenized Attribute Type (ID, IDREF, ENTITY, NMTOKEN) 6.6.2 Enumerated Attribute Types

  2. 6.1 Introduction • Document Type Definitions (DTDs) • Define structure of XML document • i.e., what elements, attributes, etc. are permitted in document • XML document not required to have DTD • Usually recommended for document conformity • Use Extended Backus-Naur Form (EBNF) grammar

  3. 6.2 Parsers, Well-formed and Valid XML Documents • Parsers • Validating • Able to read DTD • Determine whether XML document conforms to DTD • Valid document conforms to DTD • Document is then well formed, by definition • Documents can be well formed, but not valid • Nonvalidating • Able to read DTD • Cannot check document against DTD for conformity

  4. 6.3 Document Type Declaration • Document Type Declaration • Introduce DTDs into XML documents • Placed in XML document’s prolog • Begins with <!DOCTYPE • Ends with > • Can point to • External subsets • Declarations outside document • Exist in different file • typically ending with .dtd extension • Internal subsets • Declarations inside document • Visible only within document in which it resides

  5. 6.4 Element Type Declarations • Element type declarations • Declare elements in XML documents • Begin with <!ELEMENT • End with > <!ELEMENT myElement ( #PCDATA )> • myElement is generic identifier • Parentheses specify element’s content (content specification) • Keyword PCDATA • Element must contain parsable character data • All text treated as markup

  6. 1 <?xml version = "1.0"?> DOCTYPE starts document type declaration 2 Document type declaration is named myMessage 3 <!-- Fig. 6.1: intro.xml --> 4 <!-- Using an external subset --> 5 Keyword SYSTEM specifies external subset 6 <!DOCTYPE myMessage SYSTEM"intro.dtd"> intro.dtd is DTD 7 8 <myMessage> 9 <message>Welcome to XML!</message> 10 </myMessage> u Fig. 6.1 XML document declaring its associated DTD. DOCTYPE starts document type declarationDocument type declaration is named myMessageKeyword SYSTEM specifies external subsetintro.dtd is DTD

  7. 1 <!-- Fig. 6.2: intro.dtd --> Declare element myMessage 2 <!-- External declarations --> Element myMessage contains child element message 3 4 <!ELEMENT myMessage ( message )> 5 <!ELEMENT message ( #PCDATA )> Declare element message Element message contains parsable character data Fig. 6.2 Validation with using an external DTD. Declare element myMessageElement myMessage contains child element messageDeclare element messageElement message contains parsable character data

  8. Fig. 6.2 Validation with using an external DTD.

  9. Fig. 6.2 Validation with using an external DTD (cont.)

  10. 1 <?xml version = "1.0"?> 2 3 <!-- Fig. 6.3 : intro-invalid.xml --> 4 <!-- Simple introduction to XML markup --> 5 6 <!DOCTYPE myMessage SYSTEM "intro.dtd"> 7 Element myMessage’s structure does not adhere to that specified in intro.dtd 8 <!-- Root element missing child element message --> 9 <myMessage> 10 </myMessage> Fig. 6.3 Non-valid XML document. Element myMessage’s structure does not adhere to that specified in intro.dtd

  11. Fig. 6.3 Non-valid XML document.

  12. 6.4.1 Sequences, Pipe Characters and Occurrence Indicators • Sequences • Specify order in which elements occur • Comma (,) used as delimiter <!ELEMENT classroom ( teacher, student )>

  13. 6.4.1 Sequences, Pipe Characters and Occurrence Indicators (cont.) • Pipe characters (|) • Specify choices <!ELEMENT dessert ( iceCream, pastry )>

  14. 6.4.1 Sequences, Pipe Characters and Occurrence Indicators (cont.) • Occurrence indicators • Specify element’s frequency • Plus sign (+) indicates one or more occurrences <!ELEMENT album ( song+ )> • Asterisk (*) indicates optional element <!ELEMENT library ( book* )> • Question mark (?) indicates element can occur only once <!ELEMENT seat ( person? )>

  15. Fig. 6.4 Occurrence indicators.

  16. 6.4.2 EMPTY, Mixed Content and ANY • Content specification types • EMPTY • Elements do not contain character data • Elements do not contain child elements <!ELEMENT oven EMPTY> • Markup for oven element <oven/>

  17. 6.4.2 EMPTY, Mixed Content and ANY • Content specification types • Mixed content • Combination of elements and PCDATA <!ELEMENT myMessage ( #PCDATA | message )*> • Markup for myMessage <myMessage>Here is some text, some<message>other text</message>and<message>even more text</message></myMessage>

  18. 1 <?xml version = "1.0"standalone = "yes"?> Specify DTD as internal subset 2 Declare format as mixed content element 3 <!-- Fig. 6.5 : mixed.xml --> 4 <!-- Mixed content type elements --> Elements bold and italic have PCDATA only for content specification 5 6 <!DOCTYPE format [ Element format adheres to structure in DTD 7 <!ELEMENT format ( #PCDATA | bold | italic )*> 8 <!ELEMENT bold ( #PCDATA )> 9 <!ELEMENT italic ( #PCDATA )> 10 ]> 11 12 <format> 13 This is a simple formatted sentence. 14 <bold>I have tried bold.</bold> 15 <italic>I have tried italic.</italic> 16 Now what? 17 </format> Fig. 6.5 Example of a mixed-content element. Specify DTD as internal subsetDeclare format as mixed content elementElements bold and italic have PCDATA only for content specificationElement format adheres to structure in DTD

  19. Fig. 6.5 Example of a mixed-content element.

  20. Fig. 6.6 Illegal mixed-content element syntax.

  21. 6.4.2 EMPTY, Mixed Content and ANY • Content specification types • ANY • Can contain any content • PCDATA, elements or combination • Can also be empty elements • Commonly used in early DTD-development stages • Replace with specific content as DTD evolves

  22. 6.5 Attribute Declarations • Attribute declaration • Specifies element’s attribute list • Uses ATTLIST attribute list declaration

  23. 1 <?xml version = "1.0"?> Specify DTD as internal subset 2 Declare element myMessagewith child element message 3 <!-- Fig. 6.7: intro2.xml --> 4 <!-- Declaring attributes --> 5 Declare that attribute idcontain required CDATA 6 <!DOCTYPE myMessage [ 7 <!ELEMENT myMessage ( message )> 8 <!ELEMENT message ( #PCDATA )> 9 <!ATTLIST message id CDATA #REQUIRED> 10 ]> 11 12 <myMessage> 13 14 <messageid ="445"> 15 Welcome to XML! 16 </message> 17 18 </myMessage> Fig. 6.7 Declaring attributes.Specify DTD as internal subsetDeclare element myMessagewith child element messageDeclare that attribute idcontain required CDATA

  24. Fig. 6.7 Declaring attributes.

  25. 6.5.1 Attributes Defaults (#REQUIRED, #IMPLIED, #FIXED) • Attribute defaults • Specify attribute’s default value • #IMPLIED • Use (application’s) default value if attribute value not specified • #REQUIRED • Attribute must appear in element • Document is not valid if attribute is missing • #FIXED • Attribute value is constant • Attribute value cannot differ in XML document

  26. 6.6 Attribute Types • Attribute types • Strings (CDATA) • No constraints on attribute values • Except for disallowing <, >, &, ’and ” characters • Tokenized attributes • Constraints on permissible characters for attribute values • Enumerated attributes • Most restrictive • Take only one value listed in attribute declaration

  27. 6.6.1 Tokenized Attribute Type (ID, IDREF, ENTITY, NMTOKEN) • Tokenized attribute types • Restrict attribute values • ID • Uniquely identifies an element • IDREF • Points to elements with ID attribute

  28. 1 <?xml version = "1.0"?> 2 Each shipping element has a unique identifier (shipID) 3 <!-- Fig. 6.8: IDExample.xml --> 4 <!-- Example for ID and IDREF values of attributes --> 5 6 <!DOCTYPE bookstore [ 7 <!ELEMENT bookstore ( shipping+, book+ )> Attribute shippedBy points to shipping element by matching shipID attribute 8 <!ELEMENT shipping ( duration )> 9 <!ATTLIST shipping shipID ID #REQUIRED> 10 <!ELEMENT book ( #PCDATA )> 11 <!ATTLIST book shippedBy IDREF #IMPLIED> 12 <!ELEMENT duration ( #PCDATA )> 13 ]> 14 15 <bookstore> 16 <shipping shipID = "s1"> 17 <duration>2 to 4 days</duration> 18 </shipping> 19 Fig. 6.8 XML document with ID and IDREF attribute types. Each shipping element has a unique identifier (shipID)Attribute shippedBy points to shipping element by matching shipID attribute

  29. 20 <shipping shipID = "s2"> 21 <duration>1 day</duration> 22 </shipping> 23 Declare book elements with attribute shippedBy 24 <book shippedBy = "s2"> 25 Java How to Program 3rd edition. 26 </book> 27 28 <book shippedBy = "s2"> 29 C How to Program 3rd edition. 30 </book> 31 32 <book shippedBy = "s1"> 33 C++ How to Program 3rd edition. 34 </book> 35 </bookstore> Fig. 6.8 XML document with ID and IDREF attribute types . (Part 2) Declare book elements with attribute shippedBy

  30. Fig. 6.7 XML document with ID and IDREF attribute types.

  31. Assign shippedBy (line 28) value “s3” Fig. 6.9 Error displayed by XML Validator when an invalid ID is referenced. Outline Assign shippedBy (line 28) value “s3”

  32. 6.6.1 Tokenized Attribute Type (ID, IDREF, ENTITY, NMTOKEN) (cont.) • ENTITY tokenized attribute type • Indicate that attribute has entity for its value • Entity declaration <!ENTITY digits “0123456789”> • Entity may be used as follows: <useAnEntity>&digits;</useAnEntity> • Entity reference &digits; replaced by its value <useAnEntity>0123456789</useAnEntity>

  33. 1 <?xml version = "1.0"?> Declare entity city that refers to external document elements tour.html 2 3 <!-- Fig. 6.10: entityExample.xml --> 4 <!-- ENTITY and ENTITY attribute types --> NDATA indicates that external-entity content is not XML 5 6 <!DOCTYPE database [ Attribute tour for element company requires ENTITY attribute type 7 <!NOTATION html SYSTEM"iexplorer"> 8 <!ENTITY city SYSTEM"tour.html"NDATA html> 9 <!ELEMENT database ( company+ )> 10 <!ELEMENT company ( name )> 11 <!ATTLIST company tour ENTITY #REQUIRED> 12 <!ELEMENT name ( #PCDATA )> 13 ]> 14 15 <database> 16 <company tour = "city"> 17 <name>Deitel &amp; Associates, Inc.</name> 18 </company> 19 </database> Declare entity city that refers to external document elements tour.html Fig. 6.10 XML document that contains an ENTITY attribute type.Declare entity city that refers to external document elements tour.htmlNDATA indicates that external-entity content is not XMLAttribute tour for element company requires ENTITY attribute type

  34. Fig. 6.10 XML document that contains an ENTITY attribute type.

  35. Replace line 16<company tour ="city">with<company tour ="country"> Fig. 6.11 Error generated by XML Validator when a DTD contains a reference to an undefined entity. Outline Replace line 16

  36. 6.6.1 Tokenized Attribute Type (ID, IDREF, ENTITY, NMTOKEN) (cont.) • NMTOKEN tokenized attribute type • “Name token” • Value consists of letters, digits, periods, underscores, hyphens and colon characters

  37. 6.6.2 Enumerated Attribute Types • Enumerated attribute types • Declare list of possible values for attribute <!ATTLIST person gender ( M | F ) “F”> • Attribute gender can have either value M or F • F is default value