nota3-1 jan 2011
TRANSCRIPT
-
8/4/2019 nota3-1 jan 2011
1/75
DOCUMENT TYPEDEFINITION (DTD)Compiled & prepared by: NorzilahMusa
-
8/4/2019 nota3-1 jan 2011
2/75
Question?
Is there any way of making sure thatDATA is STRUCTURED in a particularway?
Yes, there is. It is necessary toensure that the data exchange
(sending/ receiving) conforms to aparticular structure. DocumentType Definition (DTD): define the
allowable structures in an XML
An XML document is not required to have acorresponding DTD.
However, DTDs are often recommended to ensuredocument conformity
-
8/4/2019 nota3-1 jan 2011
3/75
Question?
Q: What happen if I dont want thedata validated?
A: If you dont want your XML datavalidated, dont link it to a DTD.
-
8/4/2019 nota3-1 jan 2011
4/75
Parsers, Well-formed and ValidXML Documents
A validity parser is able to read the DTDand determine whether or not the XMLdocument conforms to it.
If the document conforms to the DTD, itis referred to as valid
If the document fails to conform to the
DTD but is syntactically correct, it is well-formed but not valid. By definition, a valid document is well-
formed.
-
8/4/2019 nota3-1 jan 2011
5/75
DTD : Introduction
Allows you to create rules for theelements within your XML documents.
The rules defined in a DTD are specific to
your own needs. XML document to be well-formed
- Use correct XML syntax,- Conform to its DTD or schema
The DTD is declared at the top of XMLdocument.
Contents of the DTD can be includedwithin XML document or external
-
8/4/2019 nota3-1 jan 2011
6/75
DTD : Introduction (contd)
A DTD consists of a list of syntaxdefinitions for each element in your XMLdocument.
DTD means creating a syntax rules forany XML document that uses the DTD.
Specify which element names can be
included in the document, the attributesthat each element can have, whether ornot these are required or optional, andmore.
-
8/4/2019 nota3-1 jan 2011
7/75
Why?
Each XML files can carry a description ofits own format.
Independent groups of people can agree
to use a standard DTD for interchangingdata.
An application can use a standard DTD
to verify that the data it receive from theoutside world is valid.To verify your own data.
-
8/4/2019 nota3-1 jan 2011
8/75
Example
-
8/4/2019 nota3-1 jan 2011
9/75
DTD : Internal
If the DTD is declared inside the XML file,it should be wrapped in a DOCTYPEdefinition with the following syntax:
!DOCTYPE note defines that the rootelement of this DTD
-
8/4/2019 nota3-1 jan 2011
10/75
Example
-
8/4/2019 nota3-1 jan 2011
11/75
DTD: External
ToveJani
Reminder
Don't forget me
this weekend!
(#PCDATA)>
-
8/4/2019 nota3-1 jan 2011
12/75
DTD : XML Building Blocks
All XML documents (and HTMLdocuments) are made up by thefollowing building blocks:
1) Elements main building blocks ofXML2) Attributes extra informations about
element3) Entities - characters have a specialmeaning in
XML4 PCDATA arsed character data
-
8/4/2019 nota3-1 jan 2011
13/75
DTD : XML Building Blocks(contd)
Entity References Character
< >
& &
" "
'
Predefined XML entities
-
8/4/2019 nota3-1 jan 2011
14/75
DTD : XML Building Blocks(contd)
PCDATA Parsed Character DataThink of character data as the textfound between the start tag and the
end tag of an XML element. PCDATA is text that WILL be
parsed by a parser. The text willbe examined by the parser for
entities and markup. However, parsed character data
should not contain any &, characters; these need to be
represented by the & < and& t; entities, res ectivel .
-
8/4/2019 nota3-1 jan 2011
15/75
DTD : XML Building Blocks(contd)
CDATA means character data. CDATA is text that will NOT be
parsed by a parser.Tags inside the text will NOT be
treated as markup and entities willnot be expanded.
-
8/4/2019 nota3-1 jan 2011
16/75
DTD : Elements
Elements are declared with anELEMENT declaration
Syntax:
content)>
-
8/4/2019 nota3-1 jan 2011
17/75
Element content
Specific rule1) ANY
- can contain any combination of parsabledata
- remove all syntax checking, NOTadvisable
2) Specific data or another element
- the data type/element name needs to besurrounded by brackets (i.e. (tutorial) or(#PCDATA)).
-
8/4/2019 nota3-1 jan 2011
18/75
Element content (contd)
Empty Element
Syntax :
Example:
Used for elements that do not contain content.In this case, the header element containsnothing.
-
8/4/2019 nota3-1 jan 2011
19/75
Element content (contd)
Child Elements :Specify an element that contain anotherelement, by providing the name of theelement it must contain.
Syntax :
Example:
XML tutorial
-
8/4/2019 nota3-1 jan 2011
20/75
Element content (contd)
Multiple Child Elements (Sequences)- More than one elements, used commato
separated the- This is referred to as a "sequence".- The XML document must contain thetags in the
same order that they're specified inthe sequence.
Syntax :
-
8/4/2019 nota3-1 jan 2011
21/75
Element content (contd)
Elements with only character dataElements with only character data are
declared with #PCDATA inside
parentheses: Syntax :
Example:
-
8/4/2019 nota3-1 jan 2011
22/75
Example
-
8/4/2019 nota3-1 jan 2011
23/75
Element content (contd)
Declaring only one occurrence ofthe same element
Syntax
Example:
The example declaration above declaresthat the child element message can only
occur one time inside the "note" element
-
8/4/2019 nota3-1 jan 2011
24/75
Element content (contd)
Declaring minimum oneoccurrence of the same element.
Syntax:
Example:
The + sign in the example abovedeclares that the child element messagemust occur one or more times inside the"note" element.
-
8/4/2019 nota3-1 jan 2011
25/75
Element content (contd)
-
8/4/2019 nota3-1 jan 2011
26/75
Example : Plus indicator
An elements frequency (i.e., number ofoccurrences) is specified by using either
theplus (+), astrerisk(*) or questionmark(?) occurrence indicator.
The example specifies that element
album contains one or more songelements.
-
8/4/2019 nota3-1 jan 2011
27/75
Element content (contd)
Declaring zero or more occurrencesof the same element
Example:
The * sign in the example above
declares that the child element messagecan occur zero or more times inside the"note" element.
-
8/4/2019 nota3-1 jan 2011
28/75
Example : Asterik indicator
indicates that element library containsany number ofbook elements, including
the possibility of none at all. Markupexamples that conform to this:
The nationsThe IliadThe Jungle
and
-
8/4/2019 nota3-1 jan 2011
29/75
Element content (contd)
Declaring zero or one occurrences ofthe same element
Syntax :
name?)> Example:
The ? sign in the example abovedeclares that the child element messagecan occur zero or one times inside the"note" element.
-
8/4/2019 nota3-1 jan 2011
30/75
Example : ? indicator
indicates that element seat contains atmost oneperson element. Examples of
markup that conform to this are:
Maya Karin
and
-
8/4/2019 nota3-1 jan 2011
31/75
Element content (contd)
Declaring either/or content Example:
body))>The example above declares that the
"note" element must contain a "to"element, a "from" element, a "header"element, and either a "message" or a"body" element
-
8/4/2019 nota3-1 jan 2011
32/75
Example : Pipe character (|)
Eg:
Choices are specified using thepipecharacter (|)
The example specifies that element
dessert must contain either oneicecreamelement or onepastryelement, but not both. The contentspecification may contain any number of
i e character-se arated choices.
-
8/4/2019 nota3-1 jan 2011
33/75
Example : Pipe character (|)
dessert elements contains of either a
single ice-cream element or a singlecoffee element.
A valid example:
vanilla
-
8/4/2019 nota3-1 jan 2011
34/75
Example : Pipe character (|)
Invalid example (well formed but notvalid under this element typedeclaration):
Apple
-
8/4/2019 nota3-1 jan 2011
35/75
Declaring mixed content
Example:
The example above declares that the
"note" element can contain parsedcharacter data and any number of "to","from", "header", and/or "message"elements.
-
8/4/2019 nota3-1 jan 2011
36/75
This notation is used for elements withmixed content, that is, character data andfurther elements. In this case, an order
element can consist of a mixture of atleast zero or more character data andmenu elements in any order.
For example:
The customer will have:
Chicken Soup
Apple Pie
-
8/4/2019 nota3-1 jan 2011
37/75
DTDExample.doc
-
8/4/2019 nota3-1 jan 2011
38/75
An example of markup that conforms to thisis:
XML Classical Hits
XML Overture
10
XML Symphony 1.0
54
-
8/4/2019 nota3-1 jan 2011
39/75
(credit|noCredit))>
Markup examples:123Dr. Ayip4
and456PutehMuhammad3
-
8/4/2019 nota3-1 jan 2011
40/75
Markup example:
grapehalf-sour
sourhalf-sourchocolate
and
semi-sweetwhippedsweet
-
8/4/2019 nota3-1 jan 2011
41/75
Summary
-
8/4/2019 nota3-1 jan 2011
42/75
Summary
Description of Element Type Model:
EMPTY (specifying an element with no content) ANY (specifying an element that can contain any
elements and character data)
#PCDATA (specifying a single element that contains
character data) , (specifying an element to contain sub-elements: exactly
one occurrence allowed)
| (specifying a list of sub-elements: Only one occurrence
allowed) ? (specifying a sub-element that occurs onceor not at
all)
+ (specifying sub-elements that occur at least once and
possibly more)
-
8/4/2019 nota3-1 jan 2011
43/75
-
8/4/2019 nota3-1 jan 2011
44/75
K-Break
44
-
8/4/2019 nota3-1 jan 2011
45/75
-
8/4/2019 nota3-1 jan 2011
46/75
Element Structure
Create Element or Attribute?- Particular part of document -> Element- Provide detail to an existing bit of data
-> Attribute
46
Norzilah Musa/Jul2010/CSC570
-
8/4/2019 nota3-1 jan 2011
47/75
DTD - Attributes
In a DTD, Attributes are declared with anATTLIST declaration.
Declaring Attributes
An attribute declaration has the followingsyntax:
DTD example:
XML example:
47
Norzilah Musa/Jul2010/CSC570
-
8/4/2019 nota3-1 jan 2011
48/75
Attribute list declarations
Attributes:- further describe a noun- anelement.
Attribute list declarations generally
describe four key aspects:1) The element to which the attribute isassociated2) The name of the attribute
3) The type of the attribute4) What the parser should do in case anattribute value is not supplied (that is, thedefault value)
48
Norzilah Musa/Jul2010/CSC570
-
8/4/2019 nota3-1 jan 2011
49/75
Quotes- Place quotation marks around theattribute's value
Example:
XML
Attributes : Example
Norzilah Musa/Jul2010/CSC570
49
-
8/4/2019 nota3-1 jan 2011
50/75
Attributes : Example
AnnaSmith
female
AnnaSmith
Norzilah Musa/Jul2010/CSC570
50
-
8/4/2019 nota3-1 jan 2011
51/75
Attribute TypeThe attribute-type can have the following
values:
51
Norzilah Musa/Jul2010/CSC570
-
8/4/2019 nota3-1 jan 2011
52/75
Attribute Type : CDATA
Character data, numbers, text that is notparsed.
Example
-
8/4/2019 nota3-1 jan 2011
53/75
Attribute Type : CDATA
Strings in attribute types are like stringsin all programming a series ofcharacter data. In XML, string lengthsnot defined.
The CDATA keyword implies a string.Strings can contain all text charactersexcept for special characters like
quotation marks and ampersands(symbols). BawangMerah Bawang Putih
Norzilah Musa/Jul2010/CSC570
53
-
8/4/2019 nota3-1 jan 2011
54/75
Attribute Type : NMTOKEN
Text with some restriction Numbers, letters, !xml Symbols : _ , - , . , : ! Space
Attribute Type : (value 1 |
-
8/4/2019 nota3-1 jan 2011
55/75
Attribute Type : (value-1 |value -2)
A value list a set of acceptable optionsfor the attribute to contain
Example
-
8/4/2019 nota3-1 jan 2011
56/75
Attribute Type : ID
A unique id, identifies particular element Work together with IDREF cross
referencing of elements
Example
-
8/4/2019 nota3-1 jan 2011
57/75
Attribute Type : IDREF
A unique id reference value that pointsto another elements ID value
Example
-
8/4/2019 nota3-1 jan 2011
58/75
Attribute Type : ENTITY
Attribute value is an entity An entity is a value that has been
defined elsewhere in the DTD to have
particular meaning. Example
-
8/4/2019 nota3-1 jan 2011
59/75
Attribute Type : NOTATION
Attribute value is a notation A notation is a description of how
information should be processed.
To define a helper application or plug-in ,image datatype require specific viewer,state the location, parameter
Example
-
8/4/2019 nota3-1 jan 2011
60/75
Attribute Default : Default
-
8/4/2019 nota3-1 jan 2011
61/75
Attribute Default : Defaultvalue
DTD:
Valid XML:
In the example above, the "square"
element is defined to be an emptyelement with a "width" attribute of typeCDATA. If no width is specified, it has adefault value of 0.
61
Norzilah Musa/Jul2010/CSC570
Attribute Default :
-
8/4/2019 nota3-1 jan 2011
62/75
Attribute Default :#IMPLIED
Syntax
- Optional; may be ignored if no value
Example
DTD:
Valid XML:
Valid XML:
62
Norzilah Musa/Jul2010/CSC570
-
8/4/2019 nota3-1 jan 2011
63/75
Attribute Default :
-
8/4/2019 nota3-1 jan 2011
64/75
Attribute Default :#FIXED
Syntax
type #FIXED "value">
Example
DTD:
Valid XML:
Invalid XML:
Use the #FIXED keyword when you want anattribute to have a fixed value withoutallowing the author to change it. If an authorincludes another value, the XML parser will
return an error.
64
Norzilah Musa/Jul2010/CSC570
E i
-
8/4/2019 nota3-1 jan 2011
65/75
Exercise
DTD Exercise.doc
DTD E i i
-
8/4/2019 nota3-1 jan 2011
66/75
DTD - Entities
Entity The value is entityEntities The value is a list of
entityEntities are variables used to
define shortcuts to common text.
Entity references are referencesto entities.
Entities can be declared internal,
or external
DTD Entities (contd)
-
8/4/2019 nota3-1 jan 2011
67/75
DTD Entities (cont d)
The ENTITY/ ENTITIES keyword is used inthe declaration to tell the parser to goand find the unparsed entity and pop it
into the value of the attribute.
Has three parts:1) ampersand (&)2) entity name3) semicolon (;)
Norzilah Musa/Jul2010/CSC570
67
Internal Entity
-
8/4/2019 nota3-1 jan 2011
68/75
Internal EntityDeclaration
Syntax:
DTD Example:
-
8/4/2019 nota3-1 jan 2011
69/75
Exercise
CSC570 -> April 2008, Question 1, PartB.
70Norzilah Musa/Jul2010/CSC570
-
8/4/2019 nota3-1 jan 2011
70/75
DTD - EXAMPLESFROM THE NET
TV S h d l DTD
-
8/4/2019 nota3-1 jan 2011
71/75
TV Schedule DTDBy David Moisan. Copied from his Web: http://www.davidmoisan.org/71
Norzilah Musa/Jul2010/CSC570
http://www.davidmoisan.org/http://www.davidmoisan.org/ -
8/4/2019 nota3-1 jan 2011
72/75
DTD Li it ti
-
8/4/2019 nota3-1 jan 2011
73/75
DTD Limitations
Non-XML syntax
DTD is not Extensible
Weak Data Typing
No inheritance
Possible solution: XML Schema
B i T
-
8/4/2019 nota3-1 jan 2011
74/75
Brain Teaser
How could all ofyour cousins havean aunt who is
not your aunt?
B i T
-
8/4/2019 nota3-1 jan 2011
75/75
Brain Teaser