Document Type Definitions Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University
168493: XML and Web Services (II/2546) 2 DTD: Document Type Definition Define the structure of XML documents with a list of legal elements Why use a DTD? Different groups of people can agree to use a common set of tags for interchanging data To verify that the data received from others is valid
168493: XML and Web Services (II/2546) 3 A DTD of the Sample Document
168493: XML and Web Services (II/2546) 4 Building Blocks of XML Elements Attributes Entities: define common text PCDATA: parsed character data Text that will be parsed by a parser Tags inside the text are treated as markup CDATA: character data Text is not parsed by a parser. Text is kept as it is
168493: XML and Web Services (II/2546) 5 Element Type Declarations Each element type declaration starts with <!ELEMENT Followed by the name and a content specification Its structure Example
168493: XML and Web Services (II/2546) 6 Empty Elements Empty elements are declared with keyword EMPTY DTD example: XML example: or
168493: XML and Web Services (II/2546) 7 Elements with Only Character Data Elements with only character data are declared with #PCDATA inside parentheses DTD example: XML example: Students
168493: XML and Web Services (II/2546) 8 Elements with any contents Declared with keyword ANY Can contain any combination of parsable data DTD example: XML example: Hello there
168493: XML and Web Services (II/2546) 9 Elements with Sequence Children Defined with the name of the children inside parentheses Children are declared in a sequence separated by commas Children must appear in the same sequence in the document DTD example:
168493: XML and Web Services (II/2546) 10 Occurrence Types Element with only one occurrence No symbol after the element Ex: Element with the minimum of one occurrence Symbol + after the element Ex:
168493: XML and Web Services (II/2546) 11 Occurrence Types (Cont.) Element with zero or more occurrences Symbol * after an element Ex: Element with zero or one occurrence Symbol ? after an element
168493: XML and Web Services (II/2546) 12 Elements with Content Options Provide an alternative with symbol | DTD example: XML example: Duangporn Nok
168493: XML and Web Services (II/2546) 13 Elements with Mixed Content Mixed with children and parsed character data Declared with symbol | DTD example XML example joy is a person
168493: XML and Web Services (II/2546) 14 Attributes Declarations Attribute declarations Start with the string “<!ATTLIST” Followed with an element name A list of the information of attributes The general structure
168493: XML and Web Services (II/2546) 15 Attribute Declarations ID: Identifier DTD: XML: CDATA: “character data”: not parsed DTD: XML:
168493: XML and Web Services (II/2546) 16 Attribute Defaults Attributes can have default values. For some attributes, the XML author does not need to specify an attribute value The processor can supply the default value if it exists But there are some attributes that the attribute values need to be specified
168493: XML and Web Services (II/2546) 17 Attribute Defaults (Cont.) Default values The DTD author specifies the default value Implied attributes The processor specifies the default value Required attributes The XML author specifies the default value Fixed attributes The attribute value is fixed and specified by the DTD author
168493: XML and Web Services (II/2546) 18 Default Values Include the default value after the type or list of allowed values in the attribute list declaration Examples: DTD: XML: red
168493: XML and Web Services (II/2546) 19 Impliable Attributes Allow the user to omit a value for a particular attribute without forcing a particular default Examples: some shirts are “one size fits all” DTD: XML: red Leave the values to be assigned by a processor or to be ignored
168493: XML and Web Services (II/2546) 20 Required Attributes The XML author is required to specify the attribute values A value for an attribute is important and cannot reliably be defaulted Examples DTD: XML:
168493: XML and Web Services (II/2546) 21 Fixed Attributes Attribute values cannot be overridden at all For the purpose of easy integration between documents
168493: XML and Web Services (II/2546) 22 Attribute Types Attribute value normalization CDATA and name token attributes Enumerated and notation attributes ID and IDREF attributes ENTITY attributes
168493: XML and Web Services (II/2546) 23 Attribute Values and Types The value of an attribute is not necessary the exact character string that you enter between the quotation marks The string first go through a process called attribute-value normalization Attribute types apply to the normalized value
168493: XML and Web Services (II/2546) 24 Attribute Value Normalization XML processors normalize values to make authors’ lives simpler If it were not for normalization, The XML authors must be very careful where and how they put white spaces in an attribute value All XML attribute values are entered as quoted strings
168493: XML and Web Services (II/2546) 25 Normalization Process 1. Strip off the surrounding quotes 2. Character and entity references are replaced 3. Newline characters are replaced by spaces Examples: |“ token “| |token|
168493: XML and Web Services (II/2546) 26 CDATA Attributes CDATA stands for “character data” DTD: XML: …
168493: XML and Web Services (II/2546) 27 NMTOKEN Attributes NMTOKEN: Name Token attributes Similar to CDATA but restricted in the characters that name tokens allow Name tokens are composed of strings made up of letters, numbers, and a select group of special characters Period (.), Dash (-), Underscore (_), and colon (:)
168493: XML and Web Services (II/2546) 28 NMTOKEN Examples DTD: XML: …
168493: XML and Web Services (II/2546) 29 Entity Declarations Allow you to associate a name with a fragment which can be A piece of common text A reference to an external file containing either text or binary data Three kinds of entities Internal entities External entities Parameter entities
168493: XML and Web Services (II/2546) 30 Internal Entities Associate a name with a string of literal text Examples: DTD: XML: &KKU; is in Thailand
168493: XML and Web Services (II/2546) 31 External Entities Allow an XML document to refer to the contents of another file If another file contains text, Its content is inserted at the point of reference and parsed as part of the document If another file contains binary data, Its content is not parsed May only be referenced in an attribute
168493: XML and Web Services (II/2546) 32 External Entities (Cont.) Another file is a text file Another file contains binary data, such as figures
168493: XML and Web Services (II/2546) 33 Parameter Entities Can only occur in the DTD A parameter entity declaration is identified by placing % (percent- space) in front of its name in the declaration The percent sign is also used in references to parameter entities, instead of the ampersand
168493: XML and Web Services (II/2546) 34 Parameter Entities (Cont.) Examples: Without parameter entities With parameter entities
168493: XML and Web Services (II/2546) 35 Notation Declarations Provide a name for the notation Which may allow an XML processor to locate an application capable of processing data in the given notation Example:
168493: XML and Web Services (II/2546) 36 Declaring a DTD Two ways that a DTD can be declared Inline in an XML document (Internal DOCTYPE declaration) As an external reference (External DOCTYPE declaration)
168493: XML and Web Services (II/2546) 37 Internal DOCTYPE declaration <!DOCTYPE Note [ … ]> …
168493: XML and Web Services (II/2546) 38 External DOCTYPE Declaration …