180 likes | 393 Views
Document Type Definition (DTD). Eugenia Fernandez IUPUI. Why DTDs?. DTDs define the allowable structure for an XML document. A DTD defines a vocabulary for a particular type of XML document. DTDs are not required, but recommended to ensure document conformity.
E N D
Document Type Definition(DTD) Eugenia Fernandez IUPUI
Why DTDs? • DTDs define the allowable structure for an XML document. • A DTD defines a vocabulary for a particular type of XML document. • DTDs are not required, but recommended to ensure document conformity. • They are especially useful in business-to-business exchange. • Various industries have agreed upon a common vocabulary.
Creating a DTD • A DTD describes every object (e.g. element, attribute) that can appear in an XML document. • It is a text file with a .dtd extension and can be created with any text editor: • Notepad • Visual Interdev • FrontPage
Linking a DTD To attach a DTD to an XML document, add one of the following processing instructions: <!DOCTYPE dtdname SYSTEM “dtdfilename.dtd”> <!DOCTYPE dtdname PUBLIC “url”>
What Can Be Validated: Structure • Elements • What elements are recognized? optional/required? • What are the relationships between elements? • How many child elements are allowed? What is their sequence? • What is the data type of each element? • Attributes • What attributes are recognized? optional/required? • What is the data type of each attribute? • Is there a restricted set of values for an attribute?
Declaring Elements • Syntax • Content Specification • list of elements • #PCDATA • EMPTY • ANY <!ELEMENT element-name (entry+) > content specification
Child Element List • When separated by commas • Elements must appear in the order given • Example • <!ELEMENT name (first, middle, last)> • Valid XML <name><first>Eugenia</first><middle/><last>Fernandez</last></name> • Invalid XML <name><last>Fernandez</last> <first>Eugenia</first><middle/></name>
Child Element List • When separated by vertical bar (or pipe |) • Elements are mutually exclusive, only one must appear in the document • Example • <!ELEMENT name (#PCDATA | first | last)> • Valid XML <name><first>Eugenia</first></name> <name>Eugenia</name> • Invalid XML <name><last>Fernandez</last> <first>Eugenia</first> </name>
Occurrence Indicator: + • plus, + • appears one or more times, i.e. element can repeat • Example • <!ELEMENT name (first, middle+, last)> • Valid XML <name><first>Eugenia</first><middle>Maria</middle><middle>Alicia</middle><last>Fernandez</last> </name> • Invalid XML <name><last>Fernandez</last> <first>Eugenia</first></name>
Occurrence Indicator: * • asterisk, * • appears zero or more times, i.e. element is optional and can repeat • Example • <!ELEMENT name (first, middle*, last)> • Valid XML <name><first>Eugenia</first><last>Fernandez</last> </name> <name><first>Eugenia</first><middle>Maria</middle><middle>Alicia</middle><last>Fernandez</last> </name>
Occurrence Indicator: ? • question mark, ? • appears zero or one times, i.e. element is optional and cannot repeat • Example • <!ELEMENT name (first, middle?, last)> • Valid XML <name><first>Eugenia</first><last>Fernandez</last> </name> • Invalid XML <name><first>Eugenia</first> <middle>Maria</middle><middle>Alicia</middle> <last>Fernandez</last></name>
Declaring Attributes • Syntax • Can appear anywhere in DTD • Generally placed after the element declaration <!ATTLIST element-name attribute-name attribute-value default-value>
Attribute Types • strings (CDATA) • enumerated e.g. (false | true) • tokenized • ID • IDREF • ENTITY • NMTOKEN
Tokenized Attribute Types • Restricts the values used for attributes • ID • unique identifier for each element • IDREF/IDREFS • value of an ID elsewhere in document, used to create links within a document • ENTITY/ENTITIES • name of external entity, like & for & • NMTOKEN/NMTOKENS • a word without spaces, used in enumerated values
Default Values for Attributes • #REQUIRED • attribute value must be supplied • #IMPLIED • if no value is supplied, application uses its own default • #FIXED value • attribute value must match the one specified • a literal • attribute takes on this value if none supplied
Entity Declarations • general or parameter • general entities act as shorthand or substitution macros like > • parameter entities • internal or external • internal entities stored in the document • external entities point to system or public identifier • parsed or unparsed • parsed entities contain XML text and markup • unparsed entities used for non-XML content