Download presentation
Presentation is loading. Please wait.
Published byJanice Wiggins Modified over 9 years ago
1
2002 Prentice Hall, Inc. All rights reserved. Chapter 6 – Document Type Definition (DTD) Outline 6.1Introduction 6.2Parsers, Well-formed and Valid XML Documents 6.3Document Type Declaration 6.4Element Type Declarations 6.4.1 Sequences, Pipe Characters and Occurrence Indicators 6.4.2 EMPTY, Mixed Content and ANY 6.5Attribute Declarations 6.5.1 Attribute Defaults ( #REQUIRED, #IMPLIED, #FIXED ) 6.6Attribute Types: strings (CDATA), tokenized, enumerated 6.6.1 Tokenized Attribute Type ( ID, IDREF, ENTITY, NMTOKEN ) 6.6.2 Enumerated Attribute Types 6.7Conditional Sections 6.8Whitespace Characters 6.9Case Study: Writing a DTD for the Day Planner Application
2
2002 Prentice Hall, Inc. All rights reserved. 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 Wikipedia Definition: The Backus–Naur form (also known as BNF, the Backus–Naur formalism, Backus normal form, or Panini–Backus Form) is a metasyntax used to express context- free grammars: that is, a formal way to describe formal languages.Wikipedia Definition
3
2002 Prentice Hall, Inc. All rights reserved. Simple DTD (not in the Deitel Text)
4
2002 Prentice Hall, Inc. All rights reserved. XML Document using a simple DTD This is level 1 of the nested elements This is level 2 of the nested elements
5
2002 Prentice Hall, Inc. All rights reserved. 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
6
2002 Prentice Hall, Inc. All rights reserved. 6.3 Document Type Declaration Document Type Declaration –Introduce DTDs into XML documents –Placed in XML document’s prolog (markup preceding the root element) –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
7
2002 Prentice Hall, Inc. All rights reserved. 6.3 Document Type Declaration External If the DTD is external to your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax: –Example (from intro.xml Fig 6.1)
8
2002 Prentice Hall, Inc. All rights reserved. 6.3 Document Type Declaration Internal If the DTD is included in your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax: –Example (from intro2.xml in Fig 6.7) <!DOCTYPE myMessage [ ]>
9
2002 Prentice Hall, Inc. All rights reserved. 6.3 Document Type Declaration combined Example (from p. 136 of Deitel) <!DOCTYPE myMessage SYSTEM “myDTD.dtd” [ ]>
10
2002 Prentice Hall, Inc. All rights reserved. Outline u Fig. 6.1XML document declaring its associated DTD. DOCTYPE starts document type declaration Document type declaration is named myMessage Keyword SYSTEM specifies external subset intro.dtd is DTD 1 2 3 4 5 6 7 8 9 Welcome to XML! 10 Document type declaration is named myMessage Keyword SYSTEM specifies external subset DOCTYPE starts document type declaration intro.dtd is DTD
11
2002 Prentice Hall, Inc. All rights reserved. 6.4 Element Type Declarations Element type declarations –Declare elements in XML documents –Begin with –Syntax: or myElement is generic identifier Parentheses specify element’s content (content specification) Keyword PCDATA –Element must contain parsable character data »All text treated as markup
12
2002 Prentice Hall, Inc. All rights reserved. Outline Fig. 6.2Validation with using an external DTD. Declare element myMessage Element myMessage contains child element message Declare element message Element message contains parsable character data 1 2 3 4 5 Declare element myMessage Element myMessage contains child element message Declare element message Element message contains parsable character data
13
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.2 Validation with using an external DTD.
14
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.2 Validation with using an external DTD (cont.)
15
2002 Prentice Hall, Inc. All rights reserved. Outline Fig. 6.3Non-valid XML document. Element myMessage ’s structure does not adhere to that specified in intro.dtd 1 2 3 4 5 6 7 8 9 10 Element myMessage ’s structure does not adhere to that specified in intro.dtd
16
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.3 Non-valid XML document.
17
2002 Prentice Hall, Inc. All rights reserved. 6.4.1 Sequences, Pipe Characters and Occurrence Indicators Sequences –Specify order in which elements occur –Comma (, ) used as delimiter
18
2002 Prentice Hall, Inc. All rights reserved. 6.4.1 Sequences, Pipe Characters and Occurrence Indicators (cont.) Pipe characters ( | ) –Specify choices
19
2002 Prentice Hall, Inc. All rights reserved. 6.4.1 Sequences, Pipe Characters and Occurrence Indicators (cont.) Occurrence indicators –Specify element’s frequency –Plus sign ( + ) indicates one or more occurrences –Asterisk ( * ) indicates optional element –Question mark ( ? ) indicates element can occur only once
20
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.4 Occurrence indicators.
21
2002 Prentice Hall, Inc. All rights reserved. 6.4.2 EMPTY, Mixed Content and ANY Content specification types –EMPTY Elements do not contain character data Elements do not contain child elements Markup for oven element
22
2002 Prentice Hall, Inc. All rights reserved. 6.4.2 EMPTY, Mixed Content and ANY Content specification types –Mixed content Combination of elements and PCDATA Markup for myMessage Here is some text, some other text and even more text
23
2002 Prentice Hall, Inc. All rights reserved. Outline Fig. 6.5Example of a mixed-content element. Specify DTD as internal subset Declare format as mixed content element Elements bold and italic have PCDATA only for content specification Element format adheres to structure in DTD 1 2 3 4 5 6<!DOCTYPE format [ 7 8 9 10]> 11 12 13 This is a simple formatted sentence. 14 I have tried bold. 15 I have tried italic. 16 Now what? 17 Specify DTD as internal subset Declare format as mixed content element Elements bold and italic have PCDATA only for content specification Element format adheres to structure in DTD
24
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.5 Example of a mixed-content element.
25
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.6 Illegal mixed-content element syntax.
26
2002 Prentice Hall, Inc. All rights reserved. 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
27
2002 Prentice Hall, Inc. All rights reserved. 6.5 Attribute Declarations Attribute declaration –Specifies element’s attribute list –Uses ATTLIST attribute list declaration An attribute declaration has the following syntax: DTD example: XML example:
28
2002 Prentice Hall, Inc. All rights reserved. Outline Fig. 6.7Declaring attributes. Specify DTD as internal subset Declare element myMessage with child element message Declare that attribute id contain required CDATA 1 2 3 4 5 6<!DOCTYPE myMessage [ 7 8 9 10]> 11 12 13 14 15 Welcome to XML! 16 17 18 Specify DTD as internal subset Declare element myMessage with child element message Declare that attribute id contain required CDATA
29
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.7 Declaring attributes.
30
2002 Prentice Hall, Inc. All rights reserved. 6.5.1 Attribute Defaults (#REQUIRED, #IMPLIED, #FIXED ) Attribute defaults –Specify attribute’s default value –#IMPLIED Use (application’s) default value if attribute value not specified [from w3schools: 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.] –#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
31
2002 Prentice Hall, Inc. All rights reserved. 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
32
2002 Prentice Hall, Inc. All rights reserved. 6.6 Attribute Types TypeValueExplanation StringCDATAThe value is character data Enumerated(en1|en2|…)The value must be one from an enumerated list TokenizedIDThe value is a unique id IDREFThe value is the id of another element IDREFSThe value is a list of other ids NMTOKENThe value is a valid XML name NMTOKENSThe value is a list of valid XML names ENTITYThe value is an entity ENTITIESThe value is a list of entities NOTATIONThe value is a name of a notation xml:The value is a predefined xml value
33
2002 Prentice Hall, Inc. All rights reserved. 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
34
2002 Prentice Hall, Inc. All rights reserved. Outline Fig. 6.8XML 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 1 2 3 4 5 6<!DOCTYPE bookstore [ 7 8 9 10 11 12 13]> 14 15 16 17 2 to 4 days 18 19 Each shipping element has a unique identifier ( shipID ) Attribute shippedBy points to shipping element by matching shipID attribute
35
2002 Prentice Hall, Inc. All rights reserved. Outline Fig. 6.8XML document with ID and IDREF attribute types. (Part 2) Declare book elements with attribute shippedBy 20 21 1 day 22 23 24 25 Java How to Program 3rd edition. 26 27 28 29 C How to Program 3rd edition. 30 31 32 33 C++ How to Program 3rd edition. 34 35 Declare book elements with attribute shippedBy
36
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.7 XML document with ID and IDREF attribute types.
37
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.9 Error displayed by XML Validator when an invalid ID is referenced. Assign shippedBy (line 28) value “ s3 ” Outline Assign shippedBy (line 28) value “ s3 ”
38
2002 Prentice Hall, Inc. All rights reserved. 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 may be used as follows: &digits; –Entity reference &digits; replaced by its value 0123456789
39
2002 Prentice Hall, Inc. All rights reserved. Outline Declare entity city that refers to external document elements tour.html Fig. 6.10XML document that contains an ENTITY attribute type. Declare entity city that refers to external document elements tour.html NDATA indicates that external-entity content is not XML Attribute tour for element company requires ENTITY attribute type 1 2 3 4 5 6<!DOCTYPE database [ 7 8 9 10 11 12 13]> 14 15 16 17 Deitel & Associates, Inc. 18 19 Declare entity city that refers to external document elements tour.html NDATA indicates that external- entity content is not XML Attribute tour for element company requires ENTITY attribute type
40
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.10 XML document that contains an ENTITY attribute type.
41
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.11 Error generated by XML Validator when a DTD contains a reference to an undefined entity. Replace line 16 with Outline Replace line 16
42
2002 Prentice Hall, Inc. All rights reserved. 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 (i.e. cannot contain spaces)
43
2002 Prentice Hall, Inc. All rights reserved. 6.6.2 Enumerated Attribute Types Enumerated attribute types –Declare list of possible values for attribute Attribute gender can have either value M or F F is default value
44
2002 Prentice Hall, Inc. All rights reserved. 6.7 Conditional Sections Conditional sections –Include declarations Keyword INCLUDE –Exclude declarations Keyword IGNORE –Often used with entities Parameter entities –Preceded by percent character ( % ) –Creates entities specific to DTD –Can be used only inside DTD in which they are declared
45
2002 Prentice Hall, Inc. All rights reserved. Outline Entities accept and reject represent strings INCLUDE and IGNORE, respectivelyEntities accept and reject represent strings INCLUDE and IGNORE, respectively Fig. 6.12Conditional sections in a DTD. Entities accept and reject represent strings INCLUDE and IGNORE, respectively Include this element message declaration Exclude this element message declaration 1 2 3 4 5 6 7<![ %accept; [ 8 9]]> 10 11<![ %reject; [ 12 13]]> 14 15 16 17 18 19 Entities accept and reject represent strings INCLUDE and IGNORE, respectively Include this element message declaration Exclude this element message declaration
46
2002 Prentice Hall, Inc. All rights reserved. Outline Fig. 6.13XML document that conforms to conditional.dtd. 1 2 3 4 5 6 7 8 9 10 Chairman 11
47
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.13 XML document that conforms to conditional.dtd.
48
2002 Prentice Hall, Inc. All rights reserved. Whitespace Characters Whitespace –Either preserved or normalized Depending on context in which it is used
49
2002 Prentice Hall, Inc. All rights reserved. Outline Fig. 6.14Processing whitespace in an XML document. Attribute hasCDATA requires CDATA, which preserves whitespace Other attributes normalize (do not preserve) whitespace 1 2 3 4 5 6<!DOCTYPE whitespace [ 7 <!ELEMENT whitespace ( hasCDATA, 8 hasID, hasNMTOKEN, hasEnumeration, hasMixed )> 9 10 11 12 13 14 15 16 17 18 19 20 <!ATTLIST hasEnumeration enumeration ( true | false ) 21 #REQUIRED> 22 23 24]> 25 Attribute cdata requires CDATA, which preserves whitespace Other attributes normalize (do not preserve) whitespace
50
2002 Prentice Hall, Inc. All rights reserved. Outline Fig. 6.14Processing whitespace in an XML document. (Part 2) Whitespace preserved Whitespace normalized 26 27 28 29 30 31 32 33 34 35 36 37 This is text. 38 39 This is some additional text. 40 41 42 Whitespace preservedWhitespace normalized
51
2002 Prentice Hall, Inc. All rights reserved. Outline Output from Fig. 6.14 Whitespace preserved Whitespace normalized >java Tree yes whitespace.xml URL: file:C:/Examplesps/Files/deleted/ch09/Tree/whitespace.xml [ document root ] +-[ element : whitespace ] +-[ ignorable ] +-[ ignorable ] +-[ ignorable ] +-[ element : hasCDATA ] +-[ attribute : cdata ] " simple cdata “ +-[ ignorable ] +-[ ignorable ] +-[ ignorable ] +-[ element : hasID ] +-[ attribute : id ] "i20“ +-[ ignorable ] +-[ ignorable ] +-[ ignorable ] +-[ element : hasNMTOKEN ] +-[ attribute : nmtoken ] "hello“ +-[ ignorable ] +-[ ignorable ] +-[ ignorable ] +-[ element : hasEnumeration ] +-[ attribute : enumeration ] "true“ +-[ ignorable ] +-[ ignorable ] +-[ ignorable ] +-[ element : hasMixed ] +-[ text ] “ “ +-[ text ] " This is text.“ +-[ text ] “ Whitespace normalized Whitespace preserved
52
2002 Prentice Hall, Inc. All rights reserved. Outline Output from Fig. 6.14 “ +-[ text ] " “ +-[ element : hasCDATA ] +-[ attribute : cdata ] " simple cdata“ +-[ text ] “ “ +-[ text ] " This is some additional text.“ +-[ text ] “ “ +-[ text ] " “ +-[ ignorable ] +-[ ignorable ] [ document end ]
53
2002 Prentice Hall, Inc. All rights reserved. 6.9 Case Study: Writing a DTD for the Day Planner Application Continue case study from Chapter 5 –External subset of DTD for day planner
54
2002 Prentice Hall, Inc. All rights reserved. Outline Fig. 6.15DTD for planner.xml. Root element planner Element year contains one or more date elements Element year contains attribute value that has character data Element date contains one or more note elements Element date contains attributes month and day Element note contains parsed character data and optional attribute time 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Root element planner contains any number of (optional) year elements Element year contains one or more date elements Element year contains attribute value that has character data Element date contains one or more note elements Element date contains attributes month and day, which contain has character data Element note contains parsed character data and optional attribute time
55
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.15 DTD for planner.xml.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.