Download presentation
Presentation is loading. Please wait.
1
Session III Chapter 6 – Creating DTDs http://www.profburnett.com
CMP 051 XML Introduction Session III Chapter 6 – Creating DTDs
2
Outline Introduction DTD Declaration Syntax DTD XML Building Blocks
The Need for DTDs DTD Features DTD Schematic DTD Declaration Syntax DTD XML Building Blocks DTD Elements DTD Attributes 8/1/2014 Copyright © Carl M. Burnett
3
Introduction Creates rules and structures for XML vocabulary
Document Type Definitions (DTDs) and XML schemas are vehicles for document modeling 11/9/2018 Copyright © Carl M. Burnett
4
The Need for DTDs Default attribute values Handling of white space
Part of the declaration (DTD or schema portion) must be processed To obtain correct default values Handling of white space Without the DTD, there is no way for the processor to distinguish between element content and mixed content 11/9/2018 Copyright © Carl M. Burnett
5
The Need for DTDs Authoring environments Authoring methods
Process the document type declarations for authors To comply with the content models of the document Authoring methods DTD is required for any structure to be created, understood, and maintained 11/9/2018 Copyright © Carl M. Burnett
6
DTD Features Declares a set of allowed elements
DTD forms the vocabulary of the language Defines the content model for each element Content model indicates What elements can be nested within other elements The order in which elements appear How many elements are allowed 11/9/2018 Copyright © Carl M. Burnett
7
DTD Features Declares a set of allowed attributes for each of its elements Attributes defines the name, datatype, default values and behavior Includes other mechanisms Entity declarations Notation declarations 11/9/2018 Copyright © Carl M. Burnett
8
Document Type Definition
Basic DTD Schematic Document Type Definition Prolog (optional) Element Declarations Attribute List Declarations Entity Declarations Notation Declarations Other Content 11/9/2018 Copyright © Carl M. Burnett
9
DTD Declaration Syntax
Declaration Identifier <!ELEMENT elementname content-model> Start Indicator End Indicator Reserved Keyword (Upper Case Only) 11/9/2018 Copyright © Carl M. Burnett
10
DTD - XML Building Blocks
All XML documents and HTML documents) have the following building blocks: Elements Attributes Entities PCDATA CDATA 8/1/2014 Copyright © Carl M. Burnett
11
DTD Elements Element type declarations
Specify the names of elements and nature of their content Define only one element DTD must contain as many element declarations as there are elements 11/9/2018 Copyright © Carl M. Burnett
12
Element declaration syntax
Valid XML Name (begins with a letter, colon, or underscore) <!ELEMENT elementname content-model> Start Indicator Content Model (also called content Specification) End Indicator Reserved Keyword (Upper Case Only) 11/9/2018 Copyright © Carl M. Burnett
13
XML Naming Conventions
Element names Can start with a letter, a colon, or an underscore (no numbers) Rest of the name can be alphanumeric, underscores, hyphens, colons and periods Can not contain reserved characters: ampersand (&), at symbol or less than symbol (<) Can not contain white space or use parentheses (), or brackets [] 11/9/2018 Copyright © Carl M. Burnett
14
Elements with Text - Parsed Character Data only (PCDATA)
Many Elements will only contain text. Use reserved keyword/symbol combination #PCDATA Parser checks the entity references Replaces the reference with actual entity values. <ELEMENT name (#PCDATA)> <!ELEMENT location (#PCDATA)> <!ELEMENT height (#PCDATA)> <!ELEMENT year_built (#PCDATA)> <!ELEMENT year_destroyed (#PCDATA)> <!ELEMENT how_destroyed (#PCDATA)> <!ELEMENT story (#PCDATA)> 1. Type <!ELEMENT TAG to DEFEINE tag. 2. Type (#PCDATA) to define the tag that contains a text value. 3. Type > to close out the Element definition. 11/9/2018 Copyright © Carl M. Burnett
15
Element with Empty Element
1. Type <!ELEMENT TAG to DEFEINE tag. 2. Type EMPTY to define the tag that contains an empty value. <!ELEMENT main_image EMPTY> <!ELEMENT source EMPTY> <!ELEMENT name (#PCDATA)> <!ELEMENT location (#PCDATA)> <!ELEMENT height (#PCDATA)> <!ELEMENT year_built (#PCDATA)> <!ELEMENT year_destroyed (#PCDATA)> <!ELEMENT how_destroyed (#PCDATA)> <!ELEMENT story (#PCDATA)> 3. Type > to close out the Element definition. 11/9/2018 Copyright © Carl M. Burnett
16
Element that Contain Children Elements
Elements can be parent elements with child elements. <!ELEMENT wonder (name, location, height)> <!ELEMENT name (#PCDATA)> <!ELEMENT location (#PCDATA)> <!ELEMENT height (#PCDATA)> 1. Type <!ELEMENT TAG to DEFEINE tag. 2. Type (child1, child2)to define the children elements. 3. Type > to close out the Element definition. Then define the Children elements. 11/9/2018 Copyright © Carl M. Burnett
17
Elements with Choices Contain both data and child elements
#PCDATA must be specified within the parentheses (declares the element may contain parseable data) Separate child elements with vertical line (| known as the pipe) <!ELEMENT ancient_wonders (wonder+)> <!ELEMENT wonder (#PCDATA | name | location | city | country)*> <!ELEMENT name (#PCDATA)> <!ELEMENT location (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT country (#PCDATA)> 1. Type <!ELEMENT TAG to DEFEINE tag. * Is a qualifier to allow elements to have a number of choices. 2. Type (#PCDATA | child1 | child2 | child3) to define the children elements. 3. Type > to close out the Element definition. Then define the Children elements. 11/9/2018 Copyright © Carl M. Burnett
18
Elements with No Content Restrictions
Elements with any content must use reserved keyword ANY. XML validator does not have to perform a check on the specified element’s content. <!ELEMENT ancient_wonders (wonder+)> <!ELEMENT wonder (#PCDATA | name | location | city | country)*> <!ELEMENT name (#PCDATA)> <!ELEMENT location ANY> <!ELEMENT city (#PCDATA)> <!ELEMENT country (#PCDATA)> 1. Type <!ELEMENT TAG to DEFEINE tag. 2. Type ANY to define the type of element. 3. Type > to close out the Element definition. 11/9/2018 Copyright © Carl M. Burnett
19
Operators Used with Element Content
Comma (,) specifies a required sequence of child elements Vertical line, or “pipe”, ( | ) specifies a list of candidate child elements Question mark (?) specifies that the child element is optional Plus Sign (+) specifies that at least one child element(s) is required Asterisk (*) specifies that zero or more of the child element(s) may appear 11/9/2018 Copyright © Carl M. Burnett
20
DTD Attributes In a DTD, attributes are declared with an ATTLIST declaration. In XML, there are no rules about when to use attributes, and when to use child elements. Avoid using attributes? 8/1/2014 Copyright © Carl M. Burnett
21
Avoid Using Attributes?
Some of the problems with attributes are: attributes cannot contain multiple values (child elements can) attributes are not easily expandable (for future changes) attributes cannot describe structures (child elements can) attributes are more difficult to manipulate by program code attribute values are not easy to test against a DTD If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data. 8/1/2014 Copyright © Carl M. Burnett
22
An Exception to Attribute Rule
Sometimes you can assign ID references to elements. These ID references can be used to access XML elements in much the same way as the NAME or ID attributes in HTML. 8/1/2014 Copyright © Carl M. Burnett
23
Defining Attributes Attribute Definition consist of four parts:
Elements Name Attribute Name Attribute Type Optional Status <!ELEMENT height (#PCDATA)> <!ATTLIST height units CDATA #REQUIRED> 1. Type <!ATTLIST tag where the tag is element name. 2. Type att_name where att_name is name of the attribute. 3. Type CDATA wto indicate the attribute type is text. 4. Type #IMPLIED or #REQUIRED for the optional status. The #IMPLIED can be omitted if desired. 5. Type > to close out the attribute definition. 8/1/2014 Copyright © Carl M. Burnett
24
Defining Attribute Default Values
<!ELEMENT height (#PCDATA)> <!ATTLIST height units CDATA “Default_Value”> You cannot combine the use of optional status of #IMPLIED or #REQUIRED if you provide a default value for the attribute. 8/1/2014 Copyright © Carl M. Burnett
25
Defining Attribute with Choices
1. Type <!ATTLIST tag where the tag is element name. <!ELEMENT height (#PCDATA)> <!ATTLIST height units (inches|feet) #REQUIRED> 2. Type att_name where att_name is name of the attribute. 3. Type (choice1 | choice2) where choice represents the possible value for the attribute choice. 4. Type #IMPLIED or #REQUIRED for the optional status. The #IMPLIED can be omitted if desired. 5. Type > to close out the attribute definition. 8/1/2014 Copyright © Carl M. Burnett
26
Defining Attributes with Unique Values
1. Type <!ATTLIST tag where the tag is element name. <!ELEMENT wonder (name)> <!ATTLIST wonder code ID #REQUIRED> 2. Type att_name where att_name is name of the attribute. 3. Type ID to define value of the attribute. 4. Type #IMPLIED or #REQUIRED for the optional status. The #IMPLIED can be omitted if desired. 8/1/2014 Copyright © Carl M. Burnett
27
Referencing Attributes with Unique Values
1. Type <!ATTLIST tag where the tag is element name. <!ELEMENT special_site (title, url)> <!ATTLIST special_site wonder_focus IDREF #REQUIRED> 2. Type att_name where att_name is name of the attribute. 3. Type IDREF hto define attribute that contains a value matching an existing ID value. 4. You can also type IDREFS that can contains several values separated with white spaces. <!ELEMENT general_site (title, url)> <!ATTLIST general_site contents IDREFS #REQUIRED> 5. Type #IMPLIED or #REQUIRED for the optional status. The #IMPLIED can be omitted if desired. 8/1/2014 Copyright © Carl M. Burnett
28
Restricting Attributes to Valid XML Names
1. Type <!ATTLIST tag where the tag is element name. <wonder> <w_visit primary_keyword="colossus"/> <name language="English"> Colossus of Rhodes</name> . . . primary_keyword="great pyramid"/> Great Pyramid of Giza</name> 2. Type att_name where att_name is name of the attribute. 3. Type NMTOKEN if you want the attribute value to be a valid XML name. 4. You can also type NMTOKENS if you want the attribute value to be a valid XML name based on a separated liste with white spaces. 5. Type #IMPLIED or #REQUIRED for the optional status. The #IMPLIED can be omitted if desired. 8/1/2014 Copyright © Carl M. Burnett
29
Chapter 7 - Entities and Notations in DTDs
Review Introduction The Need for DTDs DTD Features DTD Schematic DTD Declaration Syntax DTD XML Building Blocks DTD Elements DTD Attributes Next: Chapter 7 - Entities and Notations in DTDs 8/1/2014 Copyright © Carl M. Burnett
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.