38
1 Dr Alexiei Dingli XML Technologies DTD

1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

Embed Size (px)

Citation preview

Page 1: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

1

Dr Alexiei Dingli

XML Technologies

DTD

Page 2: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

2

• Document Type Definition

• Defines – the legal building blocks of an XML document– the document structure– The legal elements and attributes

• A DTD can be defined inside an xml document or outside

What’s in a DTD?

Page 3: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

3

• An XML file can carry a description of its own format

• Independent groups of people can agree to use a standard DTD for interchanging data

• Applications can use a standard DTD to verify that the data received from the outside world is valid

• A DTD can be used to verify the data

Why do we need a DTD?

Page 4: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

4

<!DOCTYPE root-element [element-declarations]>

An inside DTD (1)

Page 5: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

5

An inside DTD (2)

Page 6: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

6

• !DOCTYPE note defines that the root element of this document is note.

• !ELEMENT note defines that the note element contains four elements: "to,from,heading,body".

• !ELEMENT to defines the to element  to be of the type "#PCDATA".

• !ELEMENT from defines the from element to be of the type "#PCDATA".

• !ELEMENT heading defines the heading element to be of the type "#PCDATA".

• !ELEMENT body defines the body element to be of the type "#PCDATA".

An inside DTD (3)

Page 7: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

7

<!DOCTYPE root-element SYSTEM "filename">

<!DOCTYPE note SYSTEM "note.dtd">

External DTD

Page 8: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

8

• Elements

• Attributes

• Entities

• PCDATA

• CDATA

The building blocks

Page 9: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

9

• Main building block

• Can contain text, other elements or be empty

• The elements b, title and br can be used as follows:

<b>Hello world</b>

<title>Hello <b>Tom</b></title>

<br />

Elements

Page 10: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

10

• Provide extra information about elements

• Always placed inside the opening tag

• Always have a name/value pair

Which is the element, attribute and value?

<img src="computer.gif" />

Attributes

Page 11: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

11

Entity References Character

&lt; <

&gt; >

&amp; &

&quot; "

&apos; '

Entities

Page 12: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

12

• Text that will NOT be parsed by a parser

• Tags inside the text will NOT be treated as markup and entities will not be expanded

CDATA

Page 13: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

13

• Parsed character data• Character data is the text found between the start tag

and the end tag of an XML element• Text that WILL be parsed by a parser

– The text will be examined by the parser for entities and markup

• Tags inside the text will be treated as markup and entities will be expanded

• However, parsed character data should not contain any &, <, or > characters; these need to be represented by the &amp; &lt; and &gt; entities, respectively

PCDATA

Page 14: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

14

<!ELEMENT element-name category>

Different Elements

Page 15: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

15

<!ELEMENT br EMPTY>

<br />

The Empty Element

Page 16: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

16

<!ELEMENT element-name (#PCDATA)>

<!ELEMENT from (#PCDATA)>

Element with PCDATA

Page 17: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

17

<!ELEMENT element-name ANY>

<!ELEMENT note ANY>

Element with Any

Page 18: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

18

<!ELEMENT element-name (child1,child2,...)>

<!ELEMENT note (to,from,heading,body)>

*Sequence must be respected

Elements with Children

Page 19: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

19

<!ELEMENT element-name (child-name)>

<!ELEMENT note (message)>

Element with one child

Page 20: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

20

• One or more

– <!ELEMENT element-name (child-name+)>

– <!ELEMENT note (message+)>

• Zero or more

– <!ELEMENT element-name (child-name*)>

– <!ELEMENT note (message*)>

• Zero or One

– <!ELEMENT element-name (child-name?)>

– <!ELEMENT note (message?)>

Occurrences

Page 21: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

21

• Either or

– <!ELEMENT note (to,from,header,(message|body))>

• Mixed content

– <!ELEMENT note (#PCDATA|to|from|header|message)*>

Mixing ...

Page 22: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

22

<!ATTLIST element-name attribute-name attribute-type default-value>

<!ATTLIST payment type CDATA "check">

<payment type="check" />

Declaring Attributes

Page 23: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

23

Type Description

CDATA The value is character data

(en1|en2|..) The value must be one from an enumerated list

ID The value is a unique id

IDREF The value is the id of another element

IDREFS The value is a list of other ids

NMTOKEN The value is a valid XML name

NMTOKENS The value is a list of valid XML names

ENTITY The value is an entity

ENTITIES The value is a list of entities

NOTATION The value is a name of a notation

xml: The value is a predefined xml value

Atribute Type

Page 24: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

24

Value Explanation

value The default value of the attribute

#REQUIRED The attribute is required

#IMPLIED The attribute is not required

#FIXED value The attribute value is fixed

Attribute Value

Page 25: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

25

<!ATTLIST mountain country CDATA "New Zealand">

<mountains>

<mountain country="New Zealand">

<name>Mount Cook</name>

</mountain>

CDATA example

Page 26: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

26

<!ATTLIST tutorial published (yes | no) "no">

<tutorial published="yes">

<name>XML Tutorial</name>

</tutorial>

Enumerated Example

Page 27: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

27

<!ATTLIST mountain mountain_id ID #REQUIRED>

<mountain mountain_id="m10001">

<name>Mount Cook</name>

</mountain>

ID example

Page 28: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

28

<!ATTLIST employee employee_id ID #REQUIRED manager_id IDREF #IMPLIED>

<employees>

<employee employee_id="e10001" manager_id="e10002"> <first_name>Homer</first_name>

<last_name>Flinstone</last_name>

</employee>

<employee employee_id="e10002"> <first_name>Fred</first_name> <last_name>Burns</last_name>

</employee>

</employees>

ID Ref example

Page 29: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

29

<!ATTLIST individual individual_id ID #REQUIRED parent_id IDREFS #IMPLIED>

<individual individual_id="e10001" parent_id="e10002 e10003">

<first_name>Bart</first_name> <last_name>Simpson</last_name>

</individual>

ID Refs example

Page 30: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

30

<!ATTLIST tutorial published NMTOKEN #REQUIRED>

* Can contain only letters, digits, points, hyphen, underline and colon

<mountain country="NZ">

<name>Mount Cook</name>

</mountain>

NMTOKEN example

Page 31: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

31

<!NOTATION name SYSTEM "external_id"> * Allow you to add data which is not XML

<!NOTATION GIF SYSTEM "image/gif">

<!NOTATION JPG SYSTEM "image/jpeg">

<!NOTATION PNG SYSTEM "image/png">

<!ATTLIST mountain photo ENTITY #IMPLIED photo_type NOTATION (GIF | JPG | PNG) #IMPLIED>

Notation example

Page 32: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

32

<!ATTLIST mountain photo ENTITY #IMPLIED>

<!ENTITY mt_cook_1 SYSTEM "mt_cook1.jpg">

<mountain photo="mt_cook_1">

<name>Mount Cook</name>

</mountain>

Entity example

Page 33: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

33

• Entities are variables used to define shortcuts to standard text or special characters– Entity references are references to entities– Entities can be declared internal or external

More about Entities

Page 34: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

34

<!ENTITY writer “Alexiei Dingli">

<!ENTITY copyright "Copyright Dingli@UOM">

<author>&writer;&copyright;</author>

Internal Entities

Page 35: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

35

<!ENTITY writer SYSTEM "http://www.um.edu.mt/entities.dtd">

<!ENTITY copyright SYSTEM "http://www.um.edu.mt/entities.dtd">

<author>&writer;&copyright;</author>

External Entities

Page 36: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

36

Example

Page 37: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

37

• Define an XML Vocabulary for a Toaster Markup Language (TML)

• It must support features such as:– Print message on toast– Display on LCD– Time stamp

Exercise

Page 38: 1 Dr Alexiei Dingli XML Technologies DTD. 2 Document Type Definition Defines –the legal building blocks of an XML document –the document structure –The

38

Questions?