Download presentation
Presentation is loading. Please wait.
1
2002 Prentice Hall, Inc. All rights reserved. ISQA 407 XML/WML Winter 2002 Dr. Sergio Davalos
2
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.1Sequences, Pipe Characters and Occurrence Indicators 6.4.2 EMPTY, Mixed Content and ANY 6.5Attribute Declarations 6.5.1Attribute Defaults ( #REQUIRED, #IMPLIED, #FIXED ) 6.6Attribute Types 6.6.1Tokenized Attribute Type ( ID, IDREF, ENTITY, NMTOKEN ) 6.6.2Enumerated Attribute Types 6.7Conditional Sections 6.8Whitespace Characters 6.9Case Study: Writing a DTD for the Day Planner Application
3
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
4
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
5
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 –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
6
2002 Prentice Hall, Inc. All rights reserved. 6.4 Element Type Declarations Element type declarations –Declare elements in XML documents –Begin with <!ELEMENT –End with > myElement is generic identifier Parentheses specify element’s content (content specification) Keyword PCDATA –Element must contain parsable character data »All text treated as markup
7
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
8
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
9
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.2 Validation with using an external DTD.
10
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.2 Validation with using an external DTD (cont.)
11
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
12
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.3 Non-valid XML document.
13
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
14
2002 Prentice Hall, Inc. All rights reserved. 6.4.1 Sequences, Pipe Characters and Occurrence Indicators (cont.) Pipe characters ( | ) –Specify choices
15
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
16
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.4 Occurrence indicators.
17
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
18
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
19
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
20
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.5 Example of a mixed-content element.
21
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.6 Illegal mixed-content element syntax.
22
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
23
2002 Prentice Hall, Inc. All rights reserved. 6.5 Attribute Declarations Attribute declaration –Specifies element’s attribute list –Uses ATTLIST attribute list declaration
24
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
25
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.7 Declaring attributes.
26
2002 Prentice Hall, Inc. All rights reserved. 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
27
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
28
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
29
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
30
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
31
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.7 XML document with ID and IDREF attribute types.
32
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 ”
33
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
34
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
35
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.10 XML document that contains an ENTITY attribute type.
36
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
37
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
38
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
39
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
40
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
41
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
42
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.13 XML document that conforms to conditional.dtd.
43
2002 Prentice Hall, Inc. All rights reserved. Whitespace Characters Whitespace –Either preserved or normalized Depending on context in which it is used
44
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 hasCDATA requires CDATA, which preserves whitespace Other attributes normalize (do not preserve) whitespace
45
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
46
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
47
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 ]
48
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
49
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
50
2002 Prentice Hall, Inc. All rights reserved. Fig. 6.15 DTD for planner.xml.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.