XML's validation - DTD

956 views

Published on

Content:
- Element declarations
- Attribute declarations
- Entity declarations
References: Beginning XML, 5th Edition, Joe Fawcett, Liam R. E. Quin, Danny Ayers

Published in: Education
1 Comment
0 Likes
Statistics
Notes
  • http://www.dbmanagement.info/Tutorials/XML.htm
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total views
956
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
25
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide
  • XML trong OOP
  • To create and manage large sets of documents for your company. DTDsallow you to create and maintain rules that all documents must follow.To define clearly what markup may be used in certain documents andhow markup should be sequenced. DTDs make document rules explicit.To provide a common frame of reference for documents that manyusers can share. Big-name XML applications such as XPath have theirown associated DTDs.
  • You’re working with only one or a few small documents. Remember whyyou create DTDs: to make your life easier. If a DTD is bigger than the documentthat it describes, you may be wasting your time. You’re using a nonvalidating processor to handle your XML documents.If the processor checks only for well-formedness, you don’t need anexternal DTD.
  • An element may contain element children, text, a combination of children and text, or the element may be empty
  • Using the EMPTYkeyword, you shouldn’t declare elements that may contain content. For example, a <middle>element used to hold a middle name may or may not contain text, and therefore should not use theEMPTY keyword
  • However, the ANY keyword does not allow you to include elements that are not declared within the DTD
  • Value declaration; it controls how the XML parser handles the attribute’s value.
  • Value declaration; it controls how the XML parser handles the attribute’s value.
  • Value declaration; it controls how the XML parser handles the attribute’s value.
  • When declaring that an attribute is required, you are not permitted to specify a default value.
  • When declaring that an attribute is required, you are not permitted to specify a default value.
  • When declaring that an attribute is required, you are not permitted to specify a default value.
  • When declaring that an attribute is required, you are not permitted to specify a default value.
  • When declaring that an attribute is required, you are not permitted to specify a default value.
  • When declaring that an attribute is required, you are not permitted to specify a default value.
  • When declaring that an attribute is required, you are not permitted to specify a default value.
  • XML's validation - DTD

    1. 1. Validation - DTDs Nguyễn Đăng Khoa
    2. 2. Content • Document Type Definitions (DTDs) • XML Schemas
    3. 3. DTD – What’s a DTD? • is a set of rules that defines the elements and their attributes for an XML document • DTD defines the “grammar” for an XML document • DTDs were created as part of SGML
    4. 4. DTD’s goals • Check XML document is valid or not
    5. 5. When to use a 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
    6. 6. When NOT to use a DTD • You’re working with only one or a few small document • You’re using a nonvalidating processor to handle your XML documents
    7. 7. DTD - Example
    8. 8. DTD - Example
    9. 9. DTD – Internal subset declarations <!DOCTYPE name_of_root [ …. declarations … ]> • Declarations appear between the [ and ]
    10. 10. DTD – External subset declarations • System Identifiers <!DOCTYPE name_of_root SYSTEM “URI to DTD file” [ …. declarations … ]> – Example: • <!DOCTYPE name SYSTEM “file:///c:/name.dtd” [ ]> • <!DOCTYPE name SYSTEM “http://wiley.com/hr/name.dtd” [ ]> • <!DOCTYPE name SYSTEM “name.dtd”>
    11. 11. DTD – External subset declarations • Public Identifiers <!DOCTYPE name_of_root PUBLIC “entry in a catalog” optional_system_identifier> – Example: • <!DOCTYPE name PUBLIC “-//Beginning XML//DTD Name Example//EN”> • <!DOCTYPE name PUBLIC “-//Beginning XML//DTD Name Example//EN” “name.dtd”> – Common format is Formal Public Identifiers, FPIs -//Owner//Class Description//Language//Version
    12. 12. DTD – External subset declarations • Public Identifiers <!DOCTYPE name_of_root PUBLIC “entry in a catalog” optional_system_identifier> – Example: • <!DOCTYPE name PUBLIC “-//Beginning XML//DTD Name Example//EN”> • <!DOCTYPE name PUBLIC “-//Beginning XML//DTD Name Example//EN” “name.dtd”> – Common format is Formal Public Identifiers, FPIs -//Owner//Class Description//Language//Version <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
    13. 13. Anatomy of a DTD • Element declarations • Attribute declarations • Entity declarations
    14. 14. DTD – Element declarations • declare each element that appears within the document • can include declarations for optional elements <!ELEMENT name (first, middle, last)> ELEMENT declaration element name element content model
    15. 15. DTD – Element content models • Element • Mixed • Empty • Any
    16. 16. DTD – Element Content • Include the allowable elements within parentheses <!ELEMENT contact (name)> <!ELEMENT contact (name, location, phone, knows, description)> Sequences Choices
    17. 17. DTD – Element Content – Sequences • Elements within these documents must appear in a distinct order <!ELEMENT name (first, middle, last)> <!ELEMENT contact (name, location, phone, knows, description)>
    18. 18. DTD – Element Content – Sequences • Elements within these documents must appear in a distinct order <!ELEMENT name (first, middle, last)> <!ELEMENT contact (name, location, phone, knows, description)> Error when parent element: • is missing one of the elements • contains more elements • the elements appeared in another order
    19. 19. DTD – Element Content – Choices • Allow one element or another, but not both <!ELEMENT location (address | GPS)>
    20. 20. DTD – Element Content – Choices • Allow one element or another, but not both <!ELEMENT location (address | GPS)> Error when parent element: • is empty • contain more than one of these elements
    21. 21. DTD – Element Content – Combining Sequences and Choices • Many XML documents need to leverage much more complex rules <!ELEMENT location (address | (latitude, longitude))>
    22. 22. DTD – Mixed Content • Any element with text in its content – text can appear by itself or it can be interspersed between elements • Case 1: simplest mixed content model - text- only: <!ELEMENT first (#PCDATA)> <first>John</first>
    23. 23. DTD – Mixed Content • Case 2: Mixed content models can also contain elements interspersed within the text <description>Joe is a developer and author for <title>Beginning XML</title>, now in its <detail>5th Edition</detail></description> <!ELEMENT description (#PCDATA | title | detail)*>
    24. 24. DTD – Mixed Content • Case 2: Mixed content models can also contain elements interspersed within the text <description>Joe is a developer and author for <title>Beginning XML</title>, now in its <detail>5th Edition</detail></description> <!ELEMENT description (#PCDATA | title | detail)*> 4 rules: • They must use the choice mechanism to separate elements • The #PCDATA keyword must appear first • There must be no inner content models. • If there are child elements, the * cardinality indicator must appear at the end of the model
    25. 25. DTD – Empty Content • Elements never need to contain content <!ELEMENT br EMPTY>
    26. 26. DTD – Any Content • The ANY keyword indicates that – text (PCDATA) – any elements must be declared within the DTD – any order any number of times <!ELEMENT description ANY>
    27. 27. DTD – Example
    28. 28. DTD – Example
    29. 29. DTD – Cardinality • An element’s cardinality defines how many times it will appear within a content model
    30. 30. DTD - Example
    31. 31. DTD - Example
    32. 32. DTD - Example
    33. 33. DTD - Example
    34. 34. DTD – Attribute Declarations • declare a list of allowable attributes for each element <!ATTLIST contacts source CDATA #IMPLIED> ATTLIST declaration associated element’s name list of declared attributes
    35. 35. DTD – Attribute Declarations • declare a list of allowable attributes for each element <!ATTLIST contacts source CDATA #IMPLIED> attribute name attribute type attribute value declaration
    36. 36. DTD – Attribute Types • When declaring attributes, you can specify how the processor should handle the data that appears in the value
    37. 37. DTD – Attribute Types
    38. 38. DTD – Attribute Types – CDATA <!ATTLIST website description CDATA #IMPLIED>
    39. 39. DTD – Attribute Types – ID <!ATTLIST website url ID #IMPLIED>
    40. 40. DTD – Attribute Types – IDREF <!ATTLIST website link IDREF #IMPLIED>
    41. 41. DTD – Attribute Types – IDREFS <!ATTLIST website links IDREFS #IMPLIED>
    42. 42. DTD – Attribute Types – NMTOKEN <!ATTLIST website category NMTOKEN #IMPLIED>
    43. 43. DTD – Attribute Types – NMTOKENS <!ATTLIST website category NMTOKENS #IMPLIED>
    44. 44. DTD – Attribute Types – Enumerated list <!ATTLIST website like (YES|NO) #IMPLIED>
    45. 45. DTD – Attribute Value Declarations • Within each attribute declaration you must specify how the value will appear in the document – Has a default value – Has a fixed value – Is required – Is implied (or is optional)
    46. 46. DTD – Attribute Value Declarations – Default values • can be sure that it is included in the final output <!ATTLIST phone kind (Home | Work | Cell | Fax) “Home”> kind=“Work” kind=“Home”
    47. 47. DTD – Attribute Value Declarations – Fixed Values • When an attribute’s value can never change, you use the #FIXED keyword followed by the fixed value • Fixed values operate much like default values <!ATTLIST contacts version CDATA #FIXED “1.0”>
    48. 48. DTD – Attribute Value Declarations – Required Values • Attribute is required must be included within the XML document – you are not permitted to specify a default value <! ATTLIST phone kind (Home | Work | Cell | Fax) #REQUIRED>
    49. 49. DTD – Attribute Value Declarations – Implied Values • Attribute has no default value, has no fixed value, and is not required <! ATTLIST knows contacts IDREFS #IMPLIED>
    50. 50. DTD – Specifying Multiple Attributes <!ATTLIST contacts version CDATA #FIXED “1.0” source CDATA #IMPLIED> <!ATTLIST contacts version CDATA #FIXED “1.0”> <!ATTLIST contacts source CDATA #IMPLIED>
    51. 51. DTD – Example
    52. 52. DTD – Example
    53. 53. DTD – Example
    54. 54. DTD – Example
    55. 55. DTD – Entity Declarations • escape characters • include special characters • refer to sections of replacement text, other XML markup, and even external files
    56. 56. DTD – Entity Declarations • 4 primary types – Built-in entities – Character entities – General entities – Parameter entities
    57. 57. DTD – Entity Declarations – Built-in entities • Start with an ampersand (&) and finish with a semicolon (;) • There are five built-in entity references in XML
    58. 58. DTD – Entity Declarations – Character entities • Begin with &# and end with a semicolon (;) • Example: the Greek letter omega (Ω) as a reference it would be Ω in hexadecimal or Ω in decimal
    59. 59. DTD – Entity Declarations – General entities • create reusable sections of replacement text • must be declared within the DTD before they can be used • There are 2 ways to declare: – Internal entity declaration – External entity declaration
    60. 60. DTD – Entity Declarations – General entities • Internal Entity Declaration &source-text; &address-unknow; &empty-gps;
    61. 61. DTD – Entity Declarations – General entities • External Entity Declaration
    62. 62. DTD – Entity Declarations – Parameter entities • much like general entities, enable you to create reusable sections of replacement text • cannot be used in general content • can refer to parameter entities only within the DTD %NameDeclarations;
    63. 63. DTD – Entity Declarations – Parameter entities
    64. 64. DTD Limitations • Poor support for XML namespaces • Poor data typing • Limited content model descriptions

    ×