Document Type Definition

1,681 views
1,565 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,681
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Document Type Definition

    1. 1. Document Type Definition XML http://yht4ever.blogspot.com [email_address] B070066 - NIIT Quang Trung 07/2007
    2. 2. Contents Declare a DTD document Assign DTD to XML Document Introduction to DTD Parsers, Well-formed and valid XML Documents
    3. 3. Parsers, Well-formed and valid XML Documents <ul><li>Parsers </li></ul><ul><ul><li>Validating </li></ul></ul><ul><ul><ul><li>Able to read DTD </li></ul></ul></ul><ul><ul><ul><li>Determine whether XML document conforms to DTD </li></ul></ul></ul><ul><ul><ul><ul><li>Valid document conforms to DTD </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Document is then well formed, by definition </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Documents can be well formed, but not valid </li></ul></ul></ul></ul></ul><ul><ul><li>Nonvalidating </li></ul></ul><ul><ul><ul><li>Able to read DTD </li></ul></ul></ul><ul><ul><ul><li>Cannot check document against DTD for conformity </li></ul></ul></ul>
    4. 4. Parsers, Well-formed and valid XML Documents <ul><li>Documents must be well-formed </li></ul><ul><ul><li>Document contains single root element </li></ul></ul><ul><ul><li>Elements are balanced and properly nested </li></ul></ul><ul><ul><li>Attributes are specified and quoted </li></ul></ul><ul><ul><li>Text content contains legal XML characters </li></ul></ul><ul><li>Documents may be valid </li></ul><ul><ul><li>Document structure and content follows rules specified by grammar (e.g. DTD, XML Schema) </li></ul></ul>
    5. 5. Contents Declare a DTD document Assign DTD to XML Document Introduction to DTD Parsers, Well-formed and valid XML Documents
    6. 6. What is a DTD? <ul><li>Document Type Definition </li></ul><ul><ul><li>Defined in the XML 1.0 specification </li></ul></ul><ul><ul><li>Allows user to create new document grammars </li></ul></ul><ul><ul><ul><li>A subset borrowed from SGML </li></ul></ul></ul><ul><ul><ul><li>Uses non-XML syntax! </li></ul></ul></ul><ul><ul><li>Document-centric </li></ul></ul><ul><ul><ul><li>Focus on document structure </li></ul></ul></ul><ul><ul><ul><li>Lack of “normal” datatypes (e.g. int, float) </li></ul></ul></ul>
    7. 7. Contents Declare a DTD document Assign DTD to XML Document Introduction to DTD Parsers, Well-formed and valid XML Documents
    8. 8. Document Type Declaration <ul><li>Introduce DTDs into XML documents </li></ul><ul><li>Placed in XML document’s prolog </li></ul><ul><li>Begins with <!DOCTYPE and ends and with > </li></ul><ul><li>Can point to </li></ul><ul><ul><li>External subsets </li></ul></ul><ul><ul><ul><li>Declarations outside document </li></ul></ul></ul><ul><ul><ul><li>Exist in different file </li></ul></ul></ul><ul><ul><ul><ul><li>typically ending with .dtd extension </li></ul></ul></ul></ul><ul><ul><li>Internal subsets </li></ul></ul><ul><ul><ul><li>Declarations inside document </li></ul></ul></ul><ul><ul><ul><li>Visible only within document in which it resides </li></ul></ul></ul>
    9. 9. Document Type Declaration <ul><li>Example </li></ul><ul><ul><li><!DOCTYPE students [ </li></ul></ul><ul><ul><li><ELEMENT students (#PCDATA)> </li></ul></ul><ul><ul><li>]> </li></ul></ul><ul><ul><li><!DOCTYPE students SYSTEM “students.dtd”> </li></ul></ul><ul><ul><li><!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” http://www.w3.org/TR/html4/strict.dtd > </li></ul></ul>
    10. 10. Exercises <ul><li>Assign myschool.dtd to myschool.xml and validate it. </li></ul><ul><li>again with myschool1.xml. </li></ul>
    11. 11. Contents Declare a DTD document Assign DTD to XML Document Introduction to DTD Parsers, Well-formed and valid XML Documents
    12. 12. Element Type Definition <ul><li>Declare elements in XML documents </li></ul><ul><li>Begin with <!ELEMENT </li></ul><ul><li>End with > </li></ul><ul><li><!ELEMENT myElement ( #PCDATA ) > </li></ul><ul><ul><li>myElement is generic identifier </li></ul></ul><ul><ul><li>Parentheses specify element’s content ( content specification ) </li></ul></ul><ul><ul><li>Keyword PCDATA </li></ul></ul><ul><ul><ul><li>Element must contain parsable character data </li></ul></ul></ul><ul><li>Don’t use the same element name in multiple element type. </li></ul>
    13. 13. Sequences, Pipe Characters and Occurrence Indicators <ul><li>Sequences </li></ul><ul><ul><li>Specify order in which elements occur </li></ul></ul><ul><ul><li>Comma ( , ) used as delimiter </li></ul></ul><ul><ul><li><!ELEMENT classroom ( teacher, student ) > </li></ul></ul><ul><li>Pipe characters ( | ) </li></ul><ul><ul><li>Specify choices </li></ul></ul><ul><ul><li><!ELEMENT dessert ( ice-Cream | pastry ) > </li></ul></ul>
    14. 14. Sequences, Pipe Characters and Occurrence Indicators <ul><li>Occurrence indicators </li></ul><ul><ul><li>Specify element’s frequency </li></ul></ul><ul><ul><li>Plus sign ( + ) indicates one or more occurrences </li></ul></ul><ul><ul><ul><li><!ELEMENT album ( song+ ) > </li></ul></ul></ul><ul><ul><li>Asterisk ( * ) indicates optional element </li></ul></ul><ul><ul><ul><li><!ELEMENT library ( book* ) > </li></ul></ul></ul><ul><ul><li>Question mark ( ? ) indicates element can occur only once </li></ul></ul><ul><ul><ul><li><!ELEMENT seat ( person? ) > </li></ul></ul></ul>
    15. 15. Sequences, Pipe Characters and Occurrence Indicators
    16. 16. Content specification types <ul><li>EMPTY </li></ul><ul><ul><li>Elements do not contain character data </li></ul></ul><ul><ul><li>Elements do not contain child elements </li></ul></ul><ul><ul><li><!ELEMENT oven EMPTY> </li></ul></ul><ul><ul><li>Markup for oven element </li></ul></ul><ul><ul><li><oven/> </li></ul></ul><ul><li>Mixed content </li></ul><ul><ul><li>Combination of elements and PCDATA </li></ul></ul><ul><ul><li><!ELEMENT myMessage ( #PCDATA | message )* > </li></ul></ul><ul><ul><li>Markup for myMessage </li></ul></ul><ul><ul><li><myMessage> Here is some text, some <message> other text </message> and <message> even more text </message> </myMessage> </li></ul></ul>
    17. 17. Content specification types <ul><li>ANY </li></ul><ul><ul><li>Can contain any content </li></ul></ul><ul><ul><ul><li>PCDATA , elements or combination </li></ul></ul></ul><ul><ul><ul><li>Can also be empty elements </li></ul></ul></ul><ul><ul><li>Commonly used in early DTD-development stages </li></ul></ul><ul><ul><ul><li>Replace with specific content as DTD evolves </li></ul></ul></ul>
    18. 18. Exercises <ul><li>Create a DTD for students.xml </li></ul>
    19. 19. Attribute Type Definition <ul><li>Specifies element’s attribute list </li></ul><ul><li>Uses ATTLIST attribute list declaration </li></ul><ul><li><!ATTLIST elementname attributename valuetype [attributetype] [&quot;default&quot;]> </li></ul><ul><li>Example </li></ul><ul><li><!ELEMENT students (student*)> </li></ul><ul><li><!ELEMENT student (name, address?)> </li></ul><ul><li><!ELEMENT name (#PCDATA)> </li></ul><ul><li><!ELEMENT address (#PCDATA)> </li></ul><ul><li><!ATTLIST student id CDATA #REQUIRED> </li></ul>
    20. 20. Attribute Type Definition (cont.) <ul><li>Attribute defaults </li></ul><ul><ul><li>Specify attribute’s default value </li></ul></ul><ul><ul><li>#IMPLIED </li></ul></ul><ul><ul><ul><li>Use (application’s) default value if attribute value not specified </li></ul></ul></ul><ul><ul><li>#REQUIRED </li></ul></ul><ul><ul><ul><li>Attribute must appear in element </li></ul></ul></ul><ul><ul><ul><li>Document is not valid if attribute is missing </li></ul></ul></ul><ul><ul><li>#FIXED </li></ul></ul><ul><ul><ul><li>Attribute value is constant </li></ul></ul></ul><ul><ul><ul><li>Attribute value cannot differ in XML document </li></ul></ul></ul>
    21. 21. Attribute Type Definition (cont.) <ul><li>Example </li></ul><ul><li><!ELEMENT students (student*)> </li></ul><ul><li><!ELEMENT student (name, address?)> </li></ul><ul><li><!ELEMENT name (#PCDATA)> </li></ul><ul><li><!ELEMENT address (#PCDATA)> </li></ul><ul><li><!ATTLIST student id CDATA #REQUIRED > </li></ul><ul><li><!ATTLIST student center-id CDATA #FIXED &quot;30027&quot; > </li></ul><ul><li><!ATTLIST student country-id CDATA #IMPLIED > </li></ul>
    22. 22. Attribute Type Definition (cont.) <ul><li>Attribute types </li></ul><ul><ul><li>Strings ( CDATA ) </li></ul></ul><ul><ul><ul><li>No constraints on attribute values </li></ul></ul></ul><ul><ul><ul><ul><li>Except for disallowing < , > , & , ’ and ” characters </li></ul></ul></ul></ul><ul><ul><li>Tokenized attributes </li></ul></ul><ul><ul><ul><li>Constraints on permissible characters for attribute values </li></ul></ul></ul><ul><ul><li>Enumerated attributes </li></ul></ul><ul><ul><ul><li>Most restrictive </li></ul></ul></ul><ul><ul><ul><li>Take only one value listed in attribute declaration </li></ul></ul></ul>
    23. 23. Attribute Type Definition (cont.) <ul><li>Tokenized attribute types ( ID , IDREF , ENTITY , NMTOKEN ) </li></ul><ul><ul><li>Restrict attribute values </li></ul></ul><ul><ul><li>ID </li></ul></ul><ul><ul><ul><li>Uniquely identifies an element </li></ul></ul></ul><ul><ul><li>IDREF </li></ul></ul><ul><ul><ul><li>Points to elements with ID attribute </li></ul></ul></ul>
    24. 24. Attribute Type Definition (cont.) <ul><ul><li>ENTITY tokenized attribute type </li></ul></ul><ul><ul><ul><li>Indicate that attribute has entity for its value </li></ul></ul></ul><ul><ul><ul><li>Entity declaration </li></ul></ul></ul><ul><ul><ul><li><!ENTITY digits “0123456789” > </li></ul></ul></ul><ul><ul><ul><li>Entity may be used as follows: </li></ul></ul></ul><ul><ul><ul><li><useAnEntity> &digits; </useAnEntity> </li></ul></ul></ul><ul><ul><ul><li>Entity reference &digits; replaced by its value </li></ul></ul></ul><ul><ul><ul><li><useAnEntity> 0123456789 </useAnEntity> </li></ul></ul></ul>
    25. 25. Attribute Type Definition (cont.) <ul><li>Enumerated attribute types </li></ul><ul><ul><li>Declare list of possible values for attribute </li></ul></ul><ul><ul><li><!ATTLIST person gender ( M | F ) “F” > </li></ul></ul><ul><ul><ul><li>Attribute gender can have either value M or F </li></ul></ul></ul><ul><ul><ul><li>F is default value </li></ul></ul></ul>
    26. 26. Recursive DTDs <ul><li>We want to capture a person with a mother and a father </li></ul><ul><li>First attempt: </li></ul><ul><ul><ul><li><!ELEMENT person (name, address, person, person)> </li></ul></ul></ul><ul><ul><li>where the first person is the mother while the second is the father </li></ul></ul><ul><li>Second attempt: </li></ul><ul><ul><ul><li><!ELEMENT person (name, address, person?, person?)> </li></ul></ul></ul>
    27. 27. Recursive DTDs (cont.) <ul><li>Third attempt: </li></ul><ul><ul><ul><li><!ELEMENT person (name, address)> </li></ul></ul></ul><ul><ul><ul><li><!ATTLIST person </li></ul></ul></ul><ul><ul><ul><li>id ID #REQUIRED </li></ul></ul></ul><ul><ul><ul><li>mother IDREF #IMPLIED </li></ul></ul></ul><ul><ul><ul><li>father IDREF #IMPLIED> </li></ul></ul></ul>
    28. 28. More… <ul><li>NMTOKEN? </li></ul><ul><li><!NOTATION > ? </li></ul><ul><li>Conditional Sections? </li></ul>
    29. 29. Reference <ul><li>XML How to program </li></ul><ul><li>http://www.w3.org </li></ul><ul><li>Teach Yourself XML in 21 Days, 3 rd Edition </li></ul><ul><li>Learning XML, 2 nd Edition </li></ul><ul><li>Andy Clark presentation. </li></ul><ul><li>XML tutorial http://www.w3schools.com/w3c/ </li></ul>
    30. 30. Q&A <ul><li>Feel free to post questions at http://yht4ever.blogspot.com . </li></ul><ul><li>or email to: [email_address] or [email_address] </li></ul>
    31. 31. http://yht4ever.blogspot.com Thank You !

    ×