Document Type Definitions

1,044 views

Published on

DTD's in XML

1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
1,044
On SlideShare
0
From Embeds
0
Number of Embeds
131
Actions
Shares
0
Downloads
60
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Document Type Definitions

  1. 1. Document Type Definitions<br />XML<br />
  2. 2. DTD stands for Document Type Definition<br />Allows an XML document to go further than meeting the requirements of being well-formed<br />Specifies requirements to be valid<br />A valid XML document matches definitions of allowable elements, attributes<br />DTD Overview<br />
  3. 3. Validation can be done in code (i.e. using javascript, VB and DOM)<br />DTD’s allow use of a validating parser that compares the document against specifications<br />Typically makes application changes and maintenance easier<br />Less tied to a particular programming language/environment<br />Validation<br />
  4. 4. Declarations are used to specify document requirements<br />Document type declaration<br />Element declaration<br />Attribute List declaration<br />Entity declaration<br />Declarations<br />
  5. 5. Includes name of root element<br />Allows specification of where the DTD is located<br />DTD can be embedded in the XML file (local)<br />DTD can refer to external file, Uniform Resource Identifier (URI)<br />Local takes precedence over external<br />Document Type Declaration<br />
  6. 6. Element Declaration has 3 parts:<br />Declaration<br />Element name<br />Element content<br />Element content can include a list of child elements or data<br />Element Declaration<br />
  7. 7. DTD included in XML document<br />Definition of a student:<br />&lt;!DOCTYPE student[<br />&lt;!ELEMENT student(first, last, studentID)&gt;<br />&lt;!ELEMENT first (#PCDATA)&gt;<br />&lt;!ELEMENT last(#PCDATA)&gt;<br />&lt;ELEMENT studentID(#PCDATA)&gt;<br />]&gt;<br />LocalDTD<br />Document Type Declaration<br />Element Declaration<br />A student element is made up of first name, last name, and student id elements<br />
  8. 8. DTD exists in external file/location<br />Must use keyword to specify type of location<br />SYSTEM is a reference to local file system<br />PUBLIC is reference to DTD accessed through a catalog<br />Can use both together<br />If can’t find catalog reference can use specified file<br />External Definition<br />
  9. 9. Reference in XML file:<br />&lt;!DOCTYPE student SYSTEM “student.dtd”&gt;<br />External file:<br />&lt;!ELEMENT student(first, last, studentID)&gt;<br />&lt;!ELEMENT first (#PCDATA)&gt;<br />&lt;!ELEMENT last(#PCDATA)&gt;<br />&lt;ELEMENT studentID(#PCDATA)&gt;<br />]&gt;<br />Sample External Definition<br />Document Type Declaration<br />Element Declaration<br />
  10. 10. Element name must match name in XML document<br />If using namespaces, prefixes must match<br />Content Model defines what the element can store<br />An element<br />Mixed (i.e. data and element)<br />Empty<br />Any<br />Working With Elements<br />
  11. 11. One element can contain another<br />Can specify the elements contained by sequence<br />Can specify the elements contained as a choice<br />Element Content<br />
  12. 12. Error raised if an element is missing<br />Error raised if there are extra elements<br />Error raised if elements in a different order<br />For a student, our content must be in firstname, lastname, studentID order<br />If find an element “major”, error<br />If order varies, error<br />If missing first, last, or studentID, error<br />Content by Sequence<br />
  13. 13. Can allow content to vary between elements<br />| (vertical bar or pipe) indicates OR<br />If add a Grade element to a student that can be a letter or percent:<br />&lt;!ELEMENT grade (letter | percent)&gt;<br />&lt;!ELEMENT letter (#PCDATA)&gt;<br />&lt;!ELEMENT percent (#PCDATA)<br />Indicates that must have letter or percent element<br /> Content by Choice<br />
  14. 14. A name may be a full name (first, middle, last) or just first and last:<br />&lt;!ELEMENT name (fullName | (first, last))&gt;<br />&lt;!ELEMENT fullName (first, middle, last)&gt;<br />&lt;!ELEMENT first (#PCDATA)&gt;<br />&lt;!ELEMENT middle (#PCDATA)&gt;<br />&lt;!ELEMENT last (#PCDATA)&gt;<br />By Choice: Example<br />
  15. 15. Allows combination of elements and parsed character data<br />Can include additional information within an element, eg. how to display<br />Rules:<br />Managed by using Choice (or)<br />PCDATA must appear first in list of elements<br />List cannot include inner content model (only simple elements)<br />If there are child elements, include * <br />* Indicates that may appear zero or more times<br />Mixed Content<br />
  16. 16. If want to include emphasis with the letter grade<br />Data: &lt;letter&gt;&lt;em&gt;4&lt;/em&gt;&lt;/letter&gt;<br />Declaration:<br />&lt;!ELEMENT letter (#PCDATA | em)*&gt;<br />Describes a letter element as the content (pcdata) plus emphasis element<br />Mixed Content -2 <br />
  17. 17. An element can be empty<br />&lt;br /&gt; (never has child, content)<br />Declaration includes EMPTY:<br />&lt;!ELEMENT br EMPTY&gt;<br />Means that the element CANNOT contain content<br />Empty Content<br />
  18. 18. An element can contain any kind of value (or be empty)<br />Any elements declared in the DTD can occur, any number of times<br />Only elements that are part of the DTD can be part of the document!<br />May be empty<br />May contain PCDATA<br />Least restrictive model<br />Any Content<br />
  19. 19. How many times can an element occur?<br />How many times must an element occur?<br />Cardinality<br />
  20. 20. A student must have a first name<br />A student may or may not have a last name<br />A student may have one or more majors, or none (undeclared)<br /> &lt;!ELEMENT student (first, last?, major*)&gt;<br />Note: Cardinality indicator doesn’t affect the element declaration (i.e. major)<br />Cardinality: Example<br />
  21. 21. Elements tend to be used to describe a logical unit of information<br />Attributes are typically used to store data about characteristics (properties)<br />May have a Movie element with attributes for Title, Rental Price, Rental Days<br />No specific rules about how to use elements and attributes<br />Attributes and DTD’s<br />
  22. 22. Attributes allow more limits on data<br />Can have a list of acceptable values<br />Can have a default value<br />Some ability to specify a data type<br />Concise, about a single name/value pair<br />Attributes have limits<br />Can’t store long strings of text<br />Can’t nest values<br />Whitespace can’t be ignored<br />Attributes and Elements<br />
  23. 23. Declaration:<br />&lt;!ATTLISTElementNameAttrNameAttrType Default&gt;<br />Specify the Element the attribute belongs to<br />Specify the Name of the attribute<br />Specify the Type of data the attribute stores<br />Specify characteristics of the values (Default or attribute value)<br />List either the default value or other characteristic of value – required, optional<br />Specifying Attributes<br />
  24. 24. CDATA – unparsed character data<br />Enumerated – series/list of string values<br />Entity/Entities – reference entity definition(s)<br />ID – unique identifier for the element<br />IDREF – refer to the ID of another element<br />IDREFS – list of ID’s of other elements separated by whitespace<br />NMTOKEN/NMTOKENS – value(s) of attribute can be anything that follows rules for XML name<br />Sample Attribute Data Types<br />
  25. 25. Specifies that attribute value must be found in a particular list<br />Each value in list must be valid XML name<br />Limits on spaces, characters<br />Use | (pipe) to separate members of list<br />If specifying list letter grades for a student:<br />&lt;!ATTLIST student grade (A | B | C | D | F | V | W | I) #IMPLIED&gt;<br />Enumerated Attributes<br />Element<br />Attribute<br />Enumerated List<br />
  26. 26. An ID specifies that the element must have a unique value within the document<br />Allows reliable way to refer to a specific element<br />No spaces allowed in value<br />Typically replace space with underscore<br />Attribute list can include only one ID<br />IDREF, IDREFS allows an element to be associated with another or multiple other elements<br />A student element must have a student ID:<br />&lt;!ATTLIST student studentID ID #REQUIRED&gt;<br />ID, IDREF, IDREFS<br />
  27. 27. Attributes can refer to entities<br />“Entity” refers to substituting a reference for a text value<br />&amp; refers to the & character<br />Unparsed Entity is a reference that isn’t parsed<br />Can reuse references for long values, or hard to manage characters (i.e. tab, line feed)<br />Entity must be declared in the DTD<br />&lt;!ENTITY classTitle “XML”&gt;<br />When classTitle found in document, replaced with XML<br />Entities and Attributes<br />
  28. 28. Can specify how the value will appear in the document<br />Must always specify a value declaration<br />DEFAULT sets a value for an attribute if a value isn’t provided<br />Include default value in double quotes<br />FIXED sets a value that must occur; if an attribute has a different value, a validation error occurs<br />REQUIRED specifies that the attribute (and value) must exist<br />IMPLIED means the attribute is optional<br />Attribute Value Declarations<br />

×