Top Banner
1 XML: Document Type Definitions
36

1 XML: Document Type Definitions 2 Road Map Introduction to DTDs What’s a DTD? Why are they important? What will we cover? Our First DTD

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

1

XML: DocumentType Definitions

Page 2: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

2

Road Map

Introduction to DTDs What’s a DTD? Why are they important? What will we cover?

Our First DTD Adding Attributes Multiple Elements Creating external DTDs

Page 3: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

3

Introduction to DTDs

Page 4: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

4

Definitions

Document Type Definition: A set of rules for constructing an XML document.

Valid: Documents that conform to a DTD are said to be valid.

Page 5: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

5

Why are DTDs Important?

In order to communicate, computers need to agree to specific rules.

By agreeing to standards, lots of people/computers can share data.

Once we can share data, we can create more powerful applications.

Page 6: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

6

Case Study 1: Meerkat

What is Meerkat? “Open Wire Service” that collects news from multiple web sites. Created by OReilly.com URL: http://www.oreillynet.com/meerkat/

Uses an XML Document Type Definition, called RSS (Rich Site Summary) or ((Really Simple Syndication)

For Meerkat to work, all participating sites must adhere to the RSS DTD.

Yahoo news offer similar service:

http://news.yahoo.com/rss

Page 7: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

7

Page 8: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

8

Case Study: Meerkat

RSS DTD was developed by Netscape for my.netscape.com

Encapsulates specific article information, such as: Article Title, Article Description, Date, etc.

By agreeing to follow the RSS DTD, hundreds of web sites can easily share data.

Page 9: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

9

Example DTDs

ICE (Information and Content Exchange): enables automatic transfer of data between two or more sites.

XMLNews: standard for formatting news articles.

cXML: Commerce XML; standard for E-Commerce transactions.

BSML: Bioinformatics Sequence Markup Language.

Page 10: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

10

Example DTDs (Cont)

Chemical Markup Language: used to represent chemical molecules.

VoiceXML: used to encode text for voice recognition and synthesis.

For a more complete list, go to: http://www.xml.org/xmlorg_registry/index.shtml

Page 11: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

11

Challenge in Creating DTDs

In order to be useful, DTDs need to be widely used.

Requires that companies/organizations collaborate on the creation of DTDs.

Results can be slow and fractured. May result in competing standards. Nonetheless, many more XML DTDs are

likely to emerge within the very near future.

Page 12: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

12

What will we cover?

DTDs can become quite complicated. Our goal is to learn the basics so that you

can read basic DTD rules. We will focus on Element structure,

attributes, and internal v. external DTDs.

Page 13: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

13

Our First DTD

Page 14: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

14

Example 1: Internal DTD

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE DOCUMENT [<!ELEMENT DOCUMENT (#PCDATA)><!ENTITY Description "This is entity content.">]><DOCUMENT>This is element text and an entity follows:&Description;</DOCUMENT>

1. Prolog2. DocumentType Declaration

4. XML Document Data

3. DTD Rules

Page 15: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

15

1. Prolog

<?xml version="1.0" encoding="UTF-8"?> XML documents start with an XML prolog. Includes two major elements:

version: XML is currently in version 1.0 encoding: specifies the character encoding.

Page 16: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

16

Character Encodings

XML Parsers are required to support three types of encodings: UTF-8: Unicode Transformation - 8 bit UCS-2: Canonical Unicode Character site UTF-16: Unicode format

Why is this important? Enables internationalization of software

applications.

Page 17: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

17

2. Document Type Declaration

<!DOCTYPE DOCUMENT [

Indicates the start of the DTD. Specifies the name of the DTD. This name

should match the name of the root element.

Name of DTD

Page 18: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

18

3. DTD Rules

<!ELEMENT DOCUMENT (#PCDATA)><!ENTITY Description "This is entity content."> ELEMENT: declares a specific element.

Here, we declare a root element, called DOCUMENT.

#PCDATA: text data that does not contain any &, <, or > characters.

ENTITY: declares an abbreviation or a shortcut.

Whenever the parser sees &Description, it will replace it with "This is entity content."

Page 19: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

19

4. XML Document Data

<DOCUMENT>This is element text and an entity

follows:&Description;</DOCUMENT> Note that this document contains only one

element: DOCUMENT, and it therefore adheres the the DTD.

The document is therefore valid. When parsed by an XML Parser, the parser

will replace &Description with, "This is entity content."

Page 20: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

20

Validating your Document

You can run your document through an XML parser to determine if the document is valid.

Simple web based validator: http://www.stg.brown.edu/service/xmlvalid/ Let’s try it with our first example…

Page 21: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

21

Adding Attributes

Page 22: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

22

Adding Attributes

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE DOCUMENT [<!ELEMENT DOCUMENT (#PCDATA)><!ATTLIST DOCUMENT trackNum CDATA #REQUIRED secLevel (unclassified|classified) "unclassified"><!ENTITY Description "This is a very simple sample

document.">]><DOCUMENT trackNum="1234">This is element text

and an entity follows:&Description; </DOCUMENT>

Here, we have added twoattributes to the Documentelement.

Page 23: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

23

Attributes

<!ATTLIST ElementName AttributeName Type Default> When you create an attribute, you specify the

associated element, the attribute name, the attribute type, and any attribute defaults.

Attribute Types: CDATA: attribute may contain any text data. (this one | that one): The value of the attribute must

match one of the values listed. Other options (but, we won’t cover these...)

Page 24: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

24

Attribute Defaults

Attribute Defaults: #REQUIRED: The attribute must be specified. #IMPLIED: The attribute is optional. #FIXED value: Attribute is preset to a specific value. Defaultvalue: Provides a default value for the attribute.

Page 25: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

25

Attributes

<!ATTLIST DOCUMENT trackNum CDATA #REQUIRED secLevel (unclassified|classified) "unclassified"><!ENTITY Description "This is a very simple sample

document.">]> Here, we specify that the trackNum attribute

can contain any text and is required. We also specify that secLevel can be set to one

of two options: unclassified of classified. The default is unclassified.

Let’s run it through our validator and see what happens...

Page 26: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

26

Multiple Elements

Page 27: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

27

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE DOCUMENT [<!ELEMENT DOCUMENT

(TITLE,AUTHOR+,SUMMARY*,NOTE?)><!ATTLIST DOCUMENT trackNum CDATA #REQUIRED secLevel (unclassified|classified) "unclassified"><!ELEMENT TITLE (#PCDATA)><!ELEMENT AUTHOR (#PCDATA)><!ELEMENT SUMMARY (#PCDATA)><!ELEMENT NOTE (#PCDATA)><!ENTITY Description "This is a very simple sample document.">]><DOCUMENT trackNum="1234"><TITLE>Sample Document</TITLE><AUTHOR>Simon St.Laurent</AUTHOR><SUMMARY>This is element text and an entity

follows:&Description; </SUMMARY></DOCUMENT>

Page 28: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

28

Multiple Elements

<!ELEMENT DOCUMENT (TITLE,AUTHOR+,SUMMARY*,NOTE?)>

When you declare an element, you specify: the list of elements; rules for which elements are required; the sequence in which they appear; how many times they may appear

Page 29: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

29

Symbols for Element Structure

| Any element may appear, Requires appearance in specified

sequence ? Zero or One Occurrences* Zero or more Occurrences+ One or more Occurrences Required

Page 30: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

30

Element Structure

<!ELEMENT DOCUMENT (TITLE,AUTHOR+,SUMMARY*,NOTE?)>

Here, we have: Rigid sequence: TITLE, AUTHOR,

SUMMARY, NOTE TITLE (exactly one is required) AUTHOR (at least one must appear) SUMMARY (zero or more) NOTE (zero or one) Let’s try it on our validator…

Page 31: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

31

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE DOCUMENT [<!ELEMENT DOCUMENT (TITLE,AUTHOR+,SUMMARY*,NOTE?)><!ATTLIST DOCUMENT trackNum CDATA #REQUIRED secLevel (unclassified|classified) "unclassified"><!ELEMENT TITLE (#PCDATA)><!ELEMENT AUTHOR (FIRSTNAME, LASTNAME, (UNIVERSITY | COMPANY)?)><!ELEMENT FIRSTNAME (#PCDATA)><!ELEMENT LASTNAME (#PCDATA)><!ELEMENT UNIVERSITY (#PCDATA)><!ELEMENT COMPANY (#PCDATA)><!ELEMENT SUMMARY (#PCDATA)><!ENTITY Description "This is a very simple sample document.">]><DOCUMENT trackNum="1234"><TITLE>Sample Document</TITLE><AUTHOR>

<FIRSTNAME>Simon</FIRSTNAME><LASTNAME>St.Laurent</LASTNAME><COMPANY>XML Mania</COMPANY>

</AUTHOR><SUMMARY>This is element text and an entity follows:&Description;</SUMMARY></DOCUMENT>

Page 32: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

32

Creating External DTDs

Page 33: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

33

Internal v. External DTDs

Internal DTDs are really just for experimenting.

You really want to separate your DTD into an external file.

This enables multiple developers/ companies to all use the same DTD.

Page 34: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

34

The External DTD: simple.dtd

<!ELEMENT DOCUMENT (TITLE,AUTHOR+,SUMMARY*,NOTE?)><!ATTLIST DOCUMENT trackNum CDATA #REQUIRED secLevel (unclassified|classified) "unclassified"><!ELEMENT TITLE (#PCDATA)><!ELEMENT AUTHOR (FIRSTNAME,LASTNAME, (UNIVERSITY |

COMPANY)?)><!ELEMENT FIRSTNAME (#PCDATA)><!ELEMENT LASTNAME (#PCDATA)><!ELEMENT UNIVERSITY (#PCDATA)><!ELEMENT COMPANY (#PCDATA)><!ELEMENT SUMMARY (#PCDATA)><!ENTITY Description "This is a very simple sample

document.">

Page 35: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

35

XML Document

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE DOCUMENT SYSTEM "simple.dtd"><DOCUMENT trackNum="1234"><TITLE>Sample Document</TITLE><AUTHOR><FIRSTNAME>Simon</FIRSTNAME> <LASTNAME>St.Laurent</LASTNAME>

<COMPANY>XML Mania</COMPANY></AUTHOR><SUMMARY>This is element text and an entity follows:&Description;

</SUMMARY></DOCUMENT>

Here, we reference an external DTD: simple.dtd

Page 36: 1 XML: Document Type Definitions 2 Road Map  Introduction to DTDs  What’s a DTD?  Why are they important?  What will we cover?  Our First DTD

36

More on External DTDs

To reference a company wide DTD, use the keyword SYSTEM

<!DOCTYPE DOCUMENT SYSTEM "simple.dtd"> To reference a Public DTD, use the

keyword PUBLIC, and specify the URL of the DTD

<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">