XML For Dummies<br />Book Author : Lucinda Dykes & Ed Tittle<br />Slides Prepared By: Son TN<br />Chapter 8 :Understanding...
Contents<br />What ‘s a DTD ?<br />Inspecting the XML Prolog<br />Reading a DTD<br />Using Element Declarations<br />Decla...
8. 1. What’s a DTD ?<br />Document Type Definition (DTD)<br />Defines the syntax, grammar & semantics <br />Defines the do...
 1. What’s a DTD ? (Cont)<br />Table 8-1 deciphers some of the terms<br />
8.1.1 When to use a DTD<br />XML doesn’t require you to use a DTD<br />Including DTDs in a document is to decide whether y...
8.1.2 When NOT to use a DTD<br />You may not need to use a DTD if:<br />You’re working with only one or a few small docume...
8.2 2. Inspecting the XML Prolog<br />The XML prolog is the first thing that a processor — or human eye, for that matter —...
8.2.1 Examining the XML declaration<br />A declaration is markup that tells an XML processor what to do.<br />Declarations...
8.2.2 Discovering the DOCTYPE<br />The document type (DOCTYPE) declaration is markup that tells the processor where it can...
8.2.3 Understanding comments<br />Comments — use them and read them!<br />Use comments to include text that explains a doc...
8.2.4 Processing instructions<br />Processing instructions are like comments addressed to machines; they provide a way to ...
8.2.5 How about that white space?<br />XML considers four characters to be whitespace: the carriage return (
 or ch(13)), ...
8.3. Reading a DTD<br /><ul><li>Listing 8-2: The bookstore DTD, External Version</li></ul>&lt;?xml version=”1.0” encoding=...
 The DTD terms must be used exactly as written below; in other words, !ELEMENT, !ATTLIST, #REQUIRED, #PCDATA, and EMPTY mu...
8. 4.1 Using the EMPTY element type…<br />Empty elements are like boxes you put in place but want left empty.<br />To use ...
8.4.2 Adding mixed content<br />Mixed content allows elements to contain character data, or character data and child eleme...
Allow an element to contain both text and other elements (In that case, don’t forget the asterisk!</li></ul>&lt;!ELEMENT N...
8. 4.3 Using element content models<br />An element content model describes the child elements that an element can contain...
8. 4.4 Declaring Attributes<br />You need to include attribute-list declarations in your DTD whenever you want elements to...
8. 4.5 Discovering Entities<br />An entity declaration defines an alias for a block of text.<br />You can attach a name to...
8. 4.6 General entities<br />The XML specification supports two types of general entities:<br />Internal entities hold the...
8. 4.6 General entities (cont)<br />External entities<br />External entities help you integrate external documents and fil...
8. 4.7 Parameter entities<br />A parameter entity<br />Is an entity that is created specifically for the purpose of helpin...
8. 4.7 Parameter entities<br />External entities :<br />These kinds of parameter entities are called external parameter en...
8. 5. Understanding Notations<br />In XML, you may come across data that you would like to include in your documents that ...
8. 5. Calling a DTD<br />DTDs come in two flavors: internal and external.<br />Internal DTDs are entirely contained in the...
8. 5.1 Internal DTDs<br />If your DTD is short and simple, and you don’t need to include it in a large group of XML docume...
8. 5.2 External DTDs<br />Using external DTDs is a great idea, because you can then share a single DTD among any group of ...
8. 5.3 When to use an internal or external DTD<br />The inside view: Internal DTD subsets<br />A single file processes fas...
Upcoming SlideShare
Loading in …5
×

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

1,641
-1

Published on

it-slideshares.blogspot.com

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,641
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
43
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

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

  1. 1. XML For Dummies<br />Book Author : Lucinda Dykes & Ed Tittle<br />Slides Prepared By: Son TN<br />Chapter 8 :Understanding and Using DTDs<br />
  2. 2. Contents<br />What ‘s a DTD ?<br />Inspecting the XML Prolog<br />Reading a DTD<br />Using Element Declarations<br />Declaring Attributes<br />Understanding Notations<br />Calling a DTD<br />
  3. 3. 8. 1. What’s a DTD ?<br />Document Type Definition (DTD)<br />Defines the syntax, grammar & semantics <br />Defines the document structure<br />What Elements, Attributes, Entities, etc are permitted?<br />How are the document elements related & structured?<br />Referenced by or defined in XML documents, but it’s not XML! <br />Enables validation of XML documents using an XML Parser.<br />Can be referenced to by more than one XML document.<br />DTD’s may reference other DTD’s<br />
  4. 4. 1. What’s a DTD ? (Cont)<br />Table 8-1 deciphers some of the terms<br />
  5. 5. 8.1.1 When to use a DTD<br />XML doesn’t require you to use a DTD<br />Including DTDs in a document is to decide whether you want to jump off that particular bridge.<br />Several reasons to use DTD:<br />To create and manage large sets of documents for your company.<br />To define clearly what markup may be used in certain documents and how markup should be sequenced.<br />To provide a common frame of reference for documents that many users can share.<br />Standardization and control are what they’re all about!<br />
  6. 6. 8.1.2 When NOT to use a DTD<br />You may not need to use a DTD if:<br />You’re working with only one or a few small documents.<br />You’re using a nonvalidating processor to handle your XML documents.<br />Let the XML documents or data that you work with drive you toward or away from creating formal document descriptions.<br />
  7. 7. 8.2 2. Inspecting the XML Prolog<br />The XML prolog is the first thing that a processor — or human eye, for that matter — sees in an XML document<br />An XML prolog may include the following items<br />XML declaration<br />DOCTYPE declaration<br />Comments<br />Processing instructions<br />White space<br />The XML declaration.<br />
  8. 8. 8.2.1 Examining the XML declaration<br />A declaration is markup that tells an XML processor what to do.<br />Declarations don’t add structure or define document elements.<br />They provide instructions for a processor, such as what type of document to process and what standards to use.<br />The XML declaration can include version, encoding, and/or standalone attributes:<br />This is an XML document.<br />The version of XML is XML 1.0.<br />The character encoding is UTF-8.<br />An external document may be needed to complete the document content (standalone=”no”).<br />&lt;?xml version=”1.0” encoding=”UTF-8” standalone=”no”?&gt;<br />
  9. 9. 8.2.2 Discovering the DOCTYPE<br />The document type (DOCTYPE) declaration is markup that tells the processor where it can find a specific DTD.<br />Here’s the basic markup of a DOCTYPE declaration:<br />&lt;!DOCTYPE marks the start of the DOCTYPE declaration.<br />Books is the name of the DTD used.<br />SYSTEM “bookstore.dtd” tells the processor to fetch an external document — in this case, a file named bookstore.dtd.<br />&lt;!DOCTYPE books SYSTEM “bookstore.dtd”&gt;<br />
  10. 10. 8.2.3 Understanding comments<br />Comments — use them and read them!<br />Use comments to include text that explains a document better (humans love that sort of thing) without that text being displayed — or even processed.<br />The correct format is:<br />You have two rules to live by when you’re using comments:<br />Never nest a comment inside another element.<br />Never include - (hyphen) or -- (double hyphen) within the comment text.<br />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.<br />&lt;!-- comment text --&gt;<br />&lt;!-- Include your comment here --&gt;<br />
  11. 11. 8.2.4 Processing instructions<br />Processing instructions are like comments addressed to machines; they provide a way to send instructions to computer programs or applications.<br />All processing instructions follow this format:<br />A common example of a processing instruction in XML documents is a reference to stylesheets.<br />All processing instructions must begin with &lt;? and end with ?&gt;.<br />&lt;?name data?&gt;<br />&lt;?xml:stylesheet type=”text/css” href=”bookstore.css”?&gt;<br />
  12. 12. 8.2.5 How about that white space?<br />XML considers four characters to be whitespace: the carriage return ( or ch(13)), the linefeed ( or ch(10)), the tab( ), and the spacebar (&apos; &apos;). <br />In XML documents, there are two types of whitespace: <br />Significant whitespace is part of the document content and should be preserved.<br />Insignificant whitespace is used when editing XML documents for readability. <br />The XML specification allows you to add white space outside markup; it’s ignored when the document is processed<br />When you write markup, consider adding a line of white space between sections.<br />
  13. 13. 8.3. Reading a DTD<br /><ul><li>Listing 8-2: The bookstore DTD, External Version</li></ul>&lt;?xml version=”1.0” encoding=”UTF-8”?&gt;<br />&lt;!ELEMENT books (book+, totalCost, customer)&gt;<br />&lt;!ELEMENT book (bookInfo, salesInfo)&gt;<br />&lt;!ATTLIST book contentType (Fiction | Nonfiction) #REQUIRED format (Hardback |<br />Paperback) #REQUIRED&gt;<br />&lt;!ELEMENT bookInfo (title, author, publisher, isbn)&gt;<br />&lt;!ELEMENT title (#PCDATA)&gt;<br />&lt;!ELEMENT author (#PCDATA)&gt;<br />&lt;!ELEMENT publisher (#PCDATA)&gt;<br />&lt;!ELEMENT isbn (#PCDATA)&gt;<br />&lt;!ELEMENT salesInfo (price, itemNumber, date, source, shipping, cost)&gt;<br />&lt;!ELEMENT price (#PCDATA)&gt;<br />&lt;!ATTLIST price priceType (Retail | Wholesale) #REQUIRED&gt;<br />&lt;!ELEMENT itemNumber (#PCDATA)&gt;<br />&lt;!ELEMENT date (#PCDATA)&gt;<br />&lt;!ELEMENT source EMPTY&gt;<br />&lt;!ATTLIST source sourceType (Retail | Wholesale) #REQUIRED&gt;<br />&lt;!ELEMENT shipping (#PCDATA)&gt;<br />&lt;!ELEMENT cost (#PCDATA)&gt;<br />&lt;!ELEMENT totalCost (#PCDATA)&gt;<br />&lt;!ELEMENT customer (custNumber, lastName, firstName, address, city, state, zip,<br />phone, email)&gt;<br />&lt;!ATTLIST customer custType (newRetail | prevRetail | newWholesale |<br />prevWholesale) #REQUIRED&gt;<br />&lt;!ELEMENT custNumber (#PCDATA)&gt;<br />&lt;!ELEMENT lastName (#PCDATA)&gt;<br />&lt;!ELEMENT firstName (#PCDATA)&gt;<br />&lt;!ELEMENT address (#PCDATA)&gt;<br />&lt;!ELEMENT city (#PCDATA)&gt;<br />&lt;!ELEMENT state (#PCDATA)&gt;<br />&lt;!ELEMENT zip (#PCDATA)&gt;<br />&lt;!ELEMENT phone (#PCDATA)&gt;<br />&lt;!ELEMENT email (#PCDATA)&gt;<br /><ul><li>DTDs aren’t written in XML — they’re written in SGML and follow SGML rules.
  14. 14. The DTD terms must be used exactly as written below; in other words, !ELEMENT, !ATTLIST, #REQUIRED, #PCDATA, and EMPTY must all be capitalized.</li></ul>EMPTY must all be capitalized.<br /><ul><li> If you change the case,your DTD won’t work.</li></li></ul><li>8.4. Using Element Declarations<br />Element type declarations are important because they not only name your elements, but also define any children (nested elements) that an element might have.<br />We start with the root element for a document based on our example DTD:<br />Elements can be defined to contain four types of content, as listed in Table 8-2.<br />&lt;books&gt;<br />. . .<br />&lt;/books&gt;<br />
  15. 15. 8. 4.1 Using the EMPTY element type…<br />Empty elements are like boxes you put in place but want left empty.<br />To use them, first you have to point them out to the processor — by declaring them. Such a declaration looks like this:<br />In our example DTD, the source element is an empty element:<br />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.<br />&lt;!ELEMENT Name EMPTY&gt;<br />&lt;!ELEMENT source EMPTY&gt;<br />
  16. 16. 8.4.2 Adding mixed content<br />Mixed content allows elements to contain character data, or character data and child elements.<br />The basic structure for a mixed-content element declaration is as follows:<br />If the element contains only character data, then the structure looks like this:<br />Example<br />You can work with mixed content in one of two ways:<br /><ul><li>Use only parsed character data
  17. 17. Allow an element to contain both text and other elements (In that case, don’t forget the asterisk!</li></ul>&lt;!ELEMENT Name (#PCDATA | Child1 | Child2)*&gt;<br />&lt;!ELEMENT Name #PCDATA&gt;<br />&lt;!ELEMENT author (#PCDATA | publisher )*&gt;<br />
  18. 18. 8. 4.3 Using element content models<br />An element content model describes the child elements that an element can contain. The basic structure is :<br />Example <br />The element content model uses occurrence indicators to control the order and number of times that elements can occur.<br />&lt;!ELEMENT Name (childName)&gt;<br />&lt;!ELEMENT books (book+)&gt;<br />&lt;!ELEMENT customer (custNumber, lastName, firstName, address, city, state, zip, phone, email)&gt;<br />
  19. 19. 8. 4.4 Declaring Attributes<br />You need to include attribute-list declarations in your DTD whenever you want elements to use associated attributes.<br />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.<br />The basic format for an attribute-list declaration is:<br />Example<br />The following list defines the terms that appear in attribute-list declarations:<br />Element name<br />Attribute name<br />Datatype (CDATA, or character data , ID, IDREF, IDREFS, ENTITY, ENTITIES , NMTOKEN, NMTOKENS, NOTATION, Enumrated list<br />Default value (#REQUIRED, #IMPLIED , #FIXED , value)<br />&lt;!ATTLIST element-name attribute-name datatypedefaultvalue&gt;<br />&lt;!ATTLIST customer custType CDATA #REQUIRED&gt;<br />&lt;!ATTLIST price priceType (Retail | Wholesale) #REQUIRED&gt;<br />
  20. 20. 8. 4.5 Discovering Entities<br />An entity declaration defines an alias for a block of text.<br />You can attach a name to a specific block of text and then insert the whole block by using just one name.<br />An entity declaration in a DTD looks like this:<br />entityNameis the name of the entity and is used to call up the replacement Text in your document.<br />The two main classifications of entities are general entities and parameter entities.<br />A general entity is an abbreviation for data that becomes part of the content of an XML document.<br />A parameter entity is an abbreviation for data that becomes part of the content of a DTD.<br />&lt;!ENTITY entityName“replacementText”&gt;<br />
  21. 21. 8. 4.6 General entities<br />The XML specification supports two types of general entities:<br />Internal entities hold their values in the entity declaration<br />external entities point to an external file.<br />Internal entities<br />To declare a general internal entity, you must use the following syntax:<br />Or<br />&lt;!ENTITY entityName“replacementText”&gt;<br />&lt;!ENTITY store1 “River Valley Center”&gt;<br />Five commonly used internal entities are already defined as part of XML<br />
  22. 22. 8. 4.6 General entities (cont)<br />External entities<br />External entities help you integrate external documents and files into your XML document.<br />In general, you use them in one of two ways:<br />As a mechanism to divide your document into logical pieces.<br />To reference images, multimedia clips, and other non-XML files.<br />To declare an external entity, you use the following syntax:<br />Use the following syntax to refer to a public identifier not stored on your system<br />The benefit of using external entities is that they’re reusable.<br />They are subject to three important limitations:<br />You can’t use an entity before you define it.<br />Your entity references have to do something.<br />The entity has to refer to data that’s in the XML document.<br />&lt;!ENTITY entityNameSYSTEM “system-identifier”&gt;<br />&lt;!ENTITY entityNamePUBLIC “system-identifier”&gt;<br />
  23. 23. 8. 4.7 Parameter entities<br />A parameter entity<br />Is an entity that is created specifically for the purpose of helping you use shortcuts when you write a DTD.<br />They don’t refer to content in XML documents at all.<br />Parameter entities may also be internal or external.<br />Internal entities :<br />Internal parameter entities work well to eliminate the need to repeat commonly used element and attribute declarations.<br />Parameter entities must be declared before they can be used.<br />The general syntax for an internal parameter entity declaration:<br />External entities : <br />Use external parameter entities to carve DTDs into bite-size bits of declarations that are easy to read and manipulate.<br />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.<br />&lt;!ENTITY % entityName “replacementText”&gt;<br />
  24. 24. 8. 4.7 Parameter entities<br />External entities :<br />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.<br />&lt;-- Master DTD for book information, sales data,<br />and customer information --&gt;<br />&lt;!ENTITY % Bks SYSTEM “book.dtd”&gt;<br />&lt;!ENTITY % Sls SYSTEM “sales.dtd”&gt;<br />&lt;!ENTITY % Cust SYSTEM “customer.dtd”&gt;<br />%Bks;<br />%Sls;<br />%Cust;<br />
  25. 25. 8. 5. Understanding Notations<br />In XML, you may come across data that you would like to include in your documents that is not XML. <br />Notations allow you to include that data in your documents by describing the format it and allowing your application to recognize and handle it. <br />The format for a notation is: <br />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: <br />You can also use a &quot;public&quot; identifier, instead of &quot;system&quot;.<br />&lt;!NOTATION name system &quot;external_ID&quot;&gt; <br />&lt;!NOTATION GIF system &quot;image/gif&quot;&gt; <br />&lt;!NOTATION GIF public  &quot;-//IETF/NOSGML Media Type image/gif//EN&quot;  &quot;http://www.isi.edu/in-notes/iana/assignments/media-types/image/gif&quot;&gt; <br />
  26. 26. 8. 5. Calling a DTD<br />DTDs come in two flavors: internal and external.<br />Internal DTDs are entirely contained in the XML prolog of an XML document.<br />External DTDs are contained in an external file and are referenced in the DOCTYPE declaration of an XML document.<br />
  27. 27. 8. 5.1 Internal DTDs<br />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.<br />To add an internal DTD to your XML document, you include it within the DOCTYPE declaration<br />Example <br />&lt;!DOCTYPE rootElement [<br />... the entire DTD goes here ...<br />]<br />&lt;?xml version=”1.0” encoding=”UTF-8”?&gt;<br />&lt;!DOCTYPE books [<br />&lt;!ELEMENT books (book+, totalCost, customer)&gt;<br />&lt;!ELEMENT book (bookInfo, salesInfo)&gt;<br />...<br />]<br />&lt;books&gt;<br />&lt;book contentType=”Fiction” format=”Hardback”&gt;<br />&lt;bookInfo&gt;<br />...<br />
  28. 28. 8. 5.2 External DTDs<br />Using external DTDs is a great idea, because you can then share a single DTD among any group of XML documents.<br />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.<br />Example<br />&lt;!DOCTYPE rootElement SYSTEM dtd.url&gt;<br />&lt;?xml version=”1.0” encoding=”UTF-8” standalone=”no”?&gt;<br />&lt;!DOCTYPE books SYSTEM “bookstore.dtd”&gt;<br />&lt;books&gt;<br />...<br />
  29. 29. 8. 5.3 When to use an internal or external DTD<br />The inside view: Internal DTD subsets<br />A single file processes faster than multiple files.<br />Validity and well-formedness are kept in the same place. <br />You can use internal DTDs on a local system without connecting to the Internet<br />Calling for outside support: Referencing external DTDs<br />They’re recyclable<br />They’re versatile<br />They’re easy to change.<br />They’re timesavers.<br />Two are sometimes better than one<br />Combining DTDs isn’t much different.<br />Live by these two major rules when mixing these two types of DTDs:<br />An XML processor always reads the internal subset first.<br />Entities declared in the internal subset can be referenced in the external subset.<br />
  30. 30. 8.6 Summary<br />Defining DTDs<br />Knowing when and why to use a DTD<br />Using an XML prolog<br />Exploring an XML DTD<br />Declaring elements and their attributes<br />Declaring an entity<br />Noting notations<br />Including internal and external DTDs<br />Choosing between internal and external DTDs<br />

×