XML Document Type Definitions and the Document object model
Document Type Definition(DTD) A DTD defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes A DTD can be declared inside an XML file or as an external file
DTD as an external file XML document Jimmy Joey Message Hello The DOCTYPE definition defines the external DTD
The DTD The file note.dtd contains the DTD. The content of the file are as follows Ordering matters here PCDATA stands for parsed character data. This data is parsed by the XML parser The # is a reserved word indicator
DTD inside XML document <!DOCTYPE note [ ]> Jimmy Joey Message Hello
Why use a DTD? An XML file can carry a description of its own format If independent groups of people agree to use a standard DTD(e.g the DTD for MathML) then these groups can interchange data freely Your application can use a DTD to verify data from an external source You can verify your own data with a DTD
DTD Structure 1 A DTD is composed of a number of declarations Each declaration conforms to the markup declaration format The components of a DTD are: –Elements –Attributes –Entities –PCDATA –CDATA
DTD Structure 2(Elements) Declaring elements e.g. Elements with children Declaring exactly one occurrence of an element Declaring at least one occurrence of an element Declaring zero or more occurrences of an element Declaring zero or one occurrence of an element
DTD Structure 3(Elements) Declaring either/or content Declaring mixed content
DTD Structure 4 (Attributes) Declaring Attributes Recall from the recipe.xml example from previous lecture Flour
DTD Structure 5 (Attributes) There are other attribute types but we will not consider them here Alternative default values –#IMPLIED –#FIXED value
Sample DTDs The following DTDs describe the data in the XML files considered from the previous lecture. These files are provided here for completeness
XML file: Book.xml The XML Companion Neil Bradley $
DTD: book.dtd
XML file: article.xml XML in Action April 2, 2007 Joe Bloggs Example of XML XML is a Markup Language that allows its users to specify their own tags
DTD: article.dtd
XML file: recipe.xml Basic bread Flour Yeast Water Salt Mix all ingredients together, and knead thoroughly. Cover with a cloth, and leave for one hour in warm room. Knead again, place in a tin, and then bake in the oven.
DTD: recipe.dtd
Document Object Model(DOM) The XML DOM defines a standard way for accessing and manipulating XML documents The DOM presents an XML document as a tree structure. Everything in a XML document is treated as a node in the DOM