xml for dummies chapter 8 understanding and using dt ds it-slideshares.blogspot.com

29
XML For Dummies Book Author : Lucinda Dykes & Ed Tittle Slides Prepared By: Son TN Chapter 8 :Understanding and Using DTDs

Upload: phanleson

Post on 15-Jan-2015

1.322 views

Category:

Technology


0 download

DESCRIPTION

it-slideshares.blogspot.com

TRANSCRIPT

Page 1: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

XML For DummiesBook Author : Lucinda Dykes & Ed Tittle

Slides Prepared By: Son TNChapter 8 :Understanding and Using DTDs

Page 2: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

Contents1.What ‘s a DTD ?

2.Inspecting the XML Prolog

3.Reading a DTD

4.Using Element Declarations

5.Declaring Attributes

6.Understanding Notations

7.Calling a DTD

Page 3: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 1. What’s a DTD ?Document Type Definition (DTD)Defines the syntax, grammar & semantics Defines the document structure

What Elements, Attributes, Entities, etc are permitted?How are the document elements related & structured?

Referenced by or defined in XML documents, but it’s not XML!

Enables validation of XML documents using an XML Parser.

Can be referenced to by more than one XML document.

DTD’s may reference other DTD’s

Page 4: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

1. What’s a DTD ? (Cont)

Table 8-1 deciphers some of the terms

Page 5: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.1.1 When to use a DTDXML doesn’t require you to use a DTDIncluding DTDs in a document is to decide

whether you want to jump off that particular bridge.

Several reasons to use DTD:To create and manage large sets of documents for

your company.To define clearly what markup may be used in

certain documents and how markup should be sequenced.

To provide a common frame of reference for documents that many users can share.

Standardization and control are what they’re all about!

Page 6: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.1.2 When NOT to use a DTDYou may not need to use a DTD if:

You’re working with only one or a few small documents.

You’re using a nonvalidating processor to handle your XML documents.

Let the XML documents or data that you work with drive you toward or away from creating formal document descriptions.

Page 7: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.2 2. Inspecting the XML Prolog The XML prolog is the first thing that a processor — or human

eye, for that matter — sees in an XML document An XML prolog may include the following items

XML declaration DOCTYPE declaration Comments Processing instructions White space

The XML declaration.

Page 8: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.2.1 Examining the XML declarationA declaration is markup that tells an XML

processor what to do. Declarations don’t add structure or define document

elements. They provide instructions for a processor, such as what

type of document to process and what standards to use.The XML declaration can include version,

encoding, and/or standalone attributes:

This is an XML document. The version of XML is XML 1.0. The character encoding is UTF-8. An external document may be needed to complete the

document content (standalone=”no”).

<?xml version=”1.0” encoding=”UTF-8” standalone=”no”?>

Page 9: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.2.2 Discovering the DOCTYPEThe document type (DOCTYPE) declaration is

markup that tells the processor where it can find a specific DTD.

Here’s the basic markup of a DOCTYPE declaration:

<!DOCTYPE marks the start of the DOCTYPE declaration.Books is the name of the DTD used.SYSTEM “bookstore.dtd” tells the processor to fetch an

external document — in this case, a file named bookstore.dtd.

<!DOCTYPE books SYSTEM “bookstore.dtd”>

Page 10: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.2.3 Understanding commentsComments — use them and read them!Use comments to include text that explains a document better

(humans love that sort of thing) without that text being displayed — or even processed.

The correct format is:

You have two rules to live by when you’re using comments: Never nest a comment inside another element. Never include - (hyphen) or -- (double hyphen) within the

comment text.Using comments enables you to leave human-style instructions

(that is, comments) addressed to someone who reads the markup without disrupting the document’s structure.

<!-- comment text -->

<!-- Include your comment here -->

Page 11: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.2.4 Processing instructionsProcessing instructions are like comments addressed

to machines; they provide a way to send instructions to computer programs or applications.

All processing instructions follow this format:A common example of a processing instruction in XML

documents is a reference to stylesheets.

All processing instructions must begin with <? and end with ?>.

<?name data?>

<?xml:stylesheet type=”text/css” href=”bookstore.css”?>

Page 12: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.2.5 How about that white space?XML considers four characters to be whitespace: the

carriage return (\r or ch(13)), the linefeed (\n or ch(10)), the tab(\t), and the spacebar (' ').

In XML documents, there are two types of whitespace: Significant whitespace is part of the document content

and should be preserved. Insignificant whitespace is used when editing XML

documents for readability. The XML specification allows you to add white space

outside markup; it’s ignored when the document is processed

When you write markup, consider adding a line of white space between sections.

Page 13: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.3. Reading a DTDListing 8-2: The bookstore DTD, External Version

<?xml version=”1.0” encoding=”UTF-8”?><!ELEMENT books (book+, totalCost, customer)><!ELEMENT book (bookInfo, salesInfo)><!ATTLIST book contentType (Fiction | Nonfiction) #REQUIRED format (Hardback |Paperback) #REQUIRED><!ELEMENT bookInfo (title, author, publisher, isbn)><!ELEMENT title (#PCDATA)><!ELEMENT author (#PCDATA)><!ELEMENT publisher (#PCDATA)><!ELEMENT isbn (#PCDATA)><!ELEMENT salesInfo (price, itemNumber, date, source, shipping, cost)><!ELEMENT price (#PCDATA)><!ATTLIST price priceType (Retail | Wholesale) #REQUIRED><!ELEMENT itemNumber (#PCDATA)><!ELEMENT date (#PCDATA)><!ELEMENT source EMPTY><!ATTLIST source sourceType (Retail | Wholesale) #REQUIRED><!ELEMENT shipping (#PCDATA)><!ELEMENT cost (#PCDATA)><!ELEMENT totalCost (#PCDATA)><!ELEMENT customer (custNumber, lastName, firstName, address, city, state, zip,phone, email)><!ATTLIST customer custType (newRetail | prevRetail | newWholesale |prevWholesale) #REQUIRED><!ELEMENT custNumber (#PCDATA)><!ELEMENT lastName (#PCDATA)><!ELEMENT firstName (#PCDATA)><!ELEMENT address (#PCDATA)><!ELEMENT city (#PCDATA)><!ELEMENT state (#PCDATA)><!ELEMENT zip (#PCDATA)><!ELEMENT phone (#PCDATA)><!ELEMENT email (#PCDATA)>

• DTDs aren’t written in XML — they’re written in SGML and follow SGML rules.• The DTD terms must be used exactly as written below; in other words, !ELEMENT, !ATTLIST, #REQUIRED, #PCDATA, and EMPTY must all be capitalized.EMPTY must all be capitalized.• If you change the case, your DTD won’t work.

Page 14: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.4. Using Element Declarations Element type declarations are important because they not only name your

elements, but also define any children (nested elements) that an element might have.

We start with the root element for a document based on our example DTD:

Elements can be defined to contain four types of content, as listed in Table 8-2.

<books>. . .</books>

Page 15: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 4.1 Using the EMPTY element type…Empty elements are like boxes you put in place but want

left empty.To use them, first you have to point them out to the

processor — by declaring them. Such a declaration looks like this:

In our example DTD, the source element is an empty element:

If (on the other hand) you want your element to serve as a catch-all box that you can put anything in, you may want to use another type of content specification: ANY.

<!ELEMENT Name EMPTY>

<!ELEMENT source EMPTY>

Page 16: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.4.2 Adding mixed contentMixed content allows elements to contain character data,

or character data and child elements.The basic structure for a mixed-content element

declaration is as follows:

If the element contains only character data, then the structure looks like this:

Example

You can work with mixed content in one of two ways: Use only parsed character data Allow an element to contain both text and other elements

(In that case, don’t forget the asterisk!

<!ELEMENT Name (#PCDATA | Child1 | Child2)*>

<!ELEMENT Name #PCDATA>

<!ELEMENT author (#PCDATA | publisher )*>

Page 17: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 4.3 Using element content models An element content model describes the child elements that an

element can contain. The basic structure is :

Example

The element content model uses occurrence indicators to control the order and number of times that elements can occur.

<!ELEMENT Name (childName)>

<!ELEMENT books (book+)>

<!ELEMENT customer (custNumber, lastName, firstName, address, city, state, zip, phone, email)>

Page 18: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 4.4 Declaring Attributes You need to include attribute-list declarations in your DTD whenever

you want elements to use associated attributes. The attribute-list declaration lists all attributes that may be used

within a given element and also defines each attribute’s type and default value.

The basic format for an attribute-list declaration is:

Example

The following list defines the terms that appear in attribute-list declarations: Element name Attribute name Datatype (CDATA, or character data , ID, IDREF, IDREFS, ENTITY,

ENTITIES , NMTOKEN, NMTOKENS, NOTATION, Enumrated list

Default value (#REQUIRED, #IMPLIED , #FIXED , value)

<!ATTLIST element-name attribute-name datatype defaultvalue>

<!ATTLIST customer custType CDATA #REQUIRED>

<!ATTLIST price priceType (Retail | Wholesale) #REQUIRED>

Page 19: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 4.5 Discovering EntitiesAn entity declaration defines an alias for a block of text.You can attach a name to a specific block of text and then

insert the whole block by using just one name.An entity declaration in a DTD looks like this:

entityName is the name of the entity and is used to call up the replacement Text in your document.

The two main classifications of entities are general entities and parameter entities. A general entity is an abbreviation for data that becomes

part of the content of an XML document. A parameter entity is an abbreviation for data that

becomes part of the content of a DTD.

<!ENTITY entityName “replacementText”>

Page 20: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 4.6 General entitiesThe XML specification supports two types of general

entities: Internal entities hold their values in the entity

declaration external entities point to an external file.

Internal entities To declare a general internal entity, you must use the

following syntax:

Or

<!ENTITY entityName “replacementText”>

<!ENTITY store1 “River Valley Center”>

Five commonly used internal entities are already defined as part of XML

Page 21: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 4.6 General entities (cont) External entities

External entities help you integrate external documents and files into your XML document.

In general, you use them in one of two ways: As a mechanism to divide your document into logical pieces. To reference images, multimedia clips, and other non-XML files.

To declare an external entity, you use the following syntax:

Use the following syntax to refer to a public identifier not stored on your system

The benefit of using external entities is that they’re reusable. They are subject to three important limitations:

You can’t use an entity before you define it. Your entity references have to do something. The entity has to refer to data that’s in the XML document.

<!ENTITY entityName SYSTEM “system-identifier”>

<!ENTITY entityName PUBLIC “system-identifier”>

Page 22: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 4.7 Parameter entities A parameter entity

Is an entity that is created specifically for the purpose of helping you use shortcuts when you write a DTD.

They don’t refer to content in XML documents at all. Parameter entities may also be internal or external.

Internal entities : Internal parameter entities work well to eliminate the need to repeat

commonly used element and attribute declarations. Parameter entities must be declared before they can be used. The general syntax for an internal parameter entity declaration:

External entities : Use external parameter entities to carve DTDs into bite-size bits of

declarations that are easy to read and manipulate. You can then save each bit in a separate file and create a single

parameter entity in the master DTD that points to each individual file.

<!ENTITY % entityName “replacementText”>

Page 23: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 4.7 Parameter entitiesExternal entities :

These kinds of parameter entities are called external parameter entity references because they refer to information that’s external to the DTD in which they appear.

<-- Master DTD for book information, sales data,and customer information --><!ENTITY % Bks SYSTEM “book.dtd”><!ENTITY % Sls SYSTEM “sales.dtd”><!ENTITY % Cust SYSTEM “customer.dtd”>%Bks;%Sls;%Cust;

Page 24: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 5. Understanding Notations In XML, you may come across data that you would like to

include in your documents that is not XML. Notations allow you to include that data in your documents

by describing the format it and allowing your application to recognize and handle it.

The format for a notation is:

The name identifies the format used in the document, and the external_id identifies the notation - usually with MIME-types. For example, to include a GIF image in your XML document:

You can also use a "public" identifier, instead of "system".

<!NOTATION name system "external_ID">

<!NOTATION GIF system "image/gif">

<!NOTATION GIF public  "-//IETF/NOSGML Media Type image/gif//EN"  "http://www.isi.edu/in-notes/iana/assignments/media-types/image/gif">

Page 25: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 5. Calling a DTDDTDs come in two flavors: internal and

external.

Internal DTDs are entirely contained in the XML prolog of an XML document.

External DTDs are contained in an external file and are referenced in the DOCTYPE declaration of an XML document.

Page 26: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 5.1 Internal DTDs If your DTD is short and simple, and you don’t need to include it in

a large group of XML documents, you may want to use it as an internal DTD.

To add an internal DTD to your XML document, you include it within the DOCTYPE declaration

Example

<!DOCTYPE rootElement [... the entire DTD goes here ...]

<?xml version=”1.0” encoding=”UTF-8”?><!DOCTYPE books [<!ELEMENT books (book+, totalCost, customer)><!ELEMENT book (bookInfo, salesInfo)>...]<books><book contentType=”Fiction” format=”Hardback”><bookInfo>...

Page 27: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 5.2 External DTDsUsing external DTDs is a great idea, because you can then

share a single DTD among any group of XML documents.To use an external DTD with an XML document, simply

reference the external DTD in the DOCTYPE declaration in the XML prolog of your XML document.

Example

<!DOCTYPE rootElement SYSTEM dtd.url>

<?xml version=”1.0” encoding=”UTF-8” standalone=”no”?><!DOCTYPE books SYSTEM “bookstore.dtd”><books>...

Page 28: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8. 5.3 When to use an internal or external DTD The inside view: Internal DTD subsets

A single file processes faster than multiple files. Validity and well-formedness are kept in the same place. You can use internal DTDs on a local system without connecting to the

Internet Calling for outside support: Referencing external DTDs

They’re recyclable They’re versatile They’re easy to change. They’re timesavers.

Two are sometimes better than one Combining DTDs isn’t much different. Live by these two major rules when mixing these two types of DTDs:

An XML processor always reads the internal subset first. Entities declared in the internal subset can be referenced in the external

subset.

Page 29: Xml For Dummies   Chapter 8 Understanding And Using Dt Ds it-slideshares.blogspot.com

8.6 SummaryDefining DTDsKnowing when and why to use a DTDUsing an XML prologExploring an XML DTDDeclaring elements and their attributesDeclaring an entityNoting notationsIncluding internal and external DTDsChoosing between internal and external

DTDs