Title: Creating a DTD
1Creating a DTD
2What is a DTD?
- A DTD is a contract for an XML application
- The DTD is a text file for validating XML
documents. It has a .dtd file extension - DTDs specify what elements, attributes and
entities may appear in an XML file - DTDs also specify the order of elements
- and what each of those elements may contain
- DTDs also describe attributes and entities
3Internal vs. External DTDs
- It is possible to write internal DTDs for XML
documents, but it is not really practical - External DTDs can be leveraged among many XML
files, analogous to CSS / HTML - External DTDs are easier to write, modify, and
apply to one or more XML documents - DTDs can be generated from XML files, and used to
generate XML schema (XML Spy).
4Tips for Writing DTDs
- First, print out your XML document
- Write down the order of elements, and include all
attributes that may appear - Try drawing a tree with nodes and leafs
- Text entries appear at leafs / attributes
- EditML Pro builds very good DTDS
- XML Spy builds DTDs alphabetically
5Using Internal / External DTDs
- Declaring an internal DTD
- Writing an external DTD
- Naming an external DTD
- Declaring a personal external DTD
- Declaring a public external DTD
6Declaring an Internal DTD
- At the top of the XML document, after the XML
declaration, type - lt!DOCTYPE root
- where root corresponds to the name of the root
element in the document that the DTD will be
applied to. - Type gt to complete the DTD.
7Example Code Internal DTD
- lt?xml version1.0?gt
- lt!DOCTYPE endangered_species
- (you will put your declarations here)
- gt
- Leave room between and for document type
declarations.
8Writing an External DTD
- Create a new text file with a text editor
- Define the rules for the DTD (document type
definitions for defining elements and attributes,
and entities and notations) - Save the file as text only with a .dtd file
extension (be careful not to save as .txt)
9Example Code External DTD
- lt!ELEMENT endangered_species (animal)gt
- lt!ELEMENT animal (name, threats, weight?,
length, source, picture, subspecies)gt - lt!ELEMENT name (PCDATA)gt
- lt!ATTLIST name language (English Latin)gt
- ...
10Naming an External DTD
- Type
- if DTD is approved by a standards body
- - if DTD is not a recognized standard
- Type
- // Owner//DTD where owner identifies who wrote
or maintains the DTD - Type a space followed by a label for the DTD,
then //XX// where XX defines the language
11Naming an External DTD
- - //Liz Castro//DTD End_Species//EN//
- Liz Castro is the owner
- End_Species is the DTD description
- EN means the DTD is written in English
12Declaring a Personal External DTD
- In the XML declaration at the top of the
document, add standaloneno - Type lt!DOCTYPE root (name of root element)
- Type SYSTEM to indicate that the external DTD is
a personal, non-standardized DTD - Type file.dtd, where file.dtd is the DTD file
- Type gt to complete the document type declaration
- Personal (SYSTEM) is compared to Public DTDs
13Example Code
- lt?xml version1.0 standaloneno?gt
- lt!DOCTYPE endangered_species SYSTEM
http//www.cookwood.com/xml/examples/ - dtd_creating/end_species.dtdgt
- lt!-- an additional internal DTD is shown below--gt
-
- lt!ELEMENT continent (Asia Europe Africa)gt
14Declaring a Public External DTD
- In the XML declaration at the top of the
document, add standaloneno - Type lt!DOCTYPE root (name of root element)
- Type PUBLIC to indicate that the external DTD is
a standardized set of rules - Type DTD_name where DTD_name is the official
name of the DTD you are referencing - Type file.dtd, where file.dtd is the DTD file
- Type gt to complete the document type declaration
15Example Code
- lt?xml version1.0 standaloneno?gt
- lt!DOCTYPE endangered_species PUBLIC
- -//Liz Castro//DTD End_Species//EN//
- http//www.cookwood.com/xml/examples/
- dtd_creating/end_species.dtdgt
16Defining Elements, Attributes, Entities and
Notations in a DTD
17Defining Elements and Attributes in a DTD
- Type lt!ELEMENT tag
- Type EMPTY if no contents
- Type (contents) to specify contents
- Type (ANY) to allow any combination of elements
or text - TYPE gt to complete the element declaration
18Defining an Element to Contain Only Text
- Type lt!ELEMENT tag
- Next type (PCDATA)
- Finally type gt
19Defining an Element to Contain One Child
- Type lt!ELEMENT tag
- Type (child)
- Type gt to complete the declaration
20Defining an Element to Contain a Sequence
- Type lt!ELEMENT tag
- Type (child1
- Type (child2
- Repeat for (child-n
- Type ) to complete the sequence
- Type gt to close the declaration
- lt!ELEMENT (child1,child2,child3)gt
21Defining Choices
- Type lt!ELEMENT tag
- Type (child1
- Type to indicate that if the first element
appears that the second can not appear - Type child2
- Repeat for additional choices
- Type ) to complete the list of choices
- Type gt to complete the element declaration
22Defining How Many Units
- To define how many units
- Type ? to indicate that the unit can appear at
most once, if at all. - Type to indicate that the unit must appear at
least once, but is unbounded. - Type to indicate that the unit can appear as
many times as necessary, or not at all. - lt!ELEMENT (child1?,child2,child3)gt
23About Attributes
- Attributes add information about an element and
the content of that element - Information contained in attributes tends to be
about the content of the document - Elements are perhaps a better choice for
information you may want to display (css) - attributes are information about information
24Defining Simple Attributes
- Type lt!ATTLIST tag
- Type attribute
- Type CDATA (with no parenthesis
- Or type (choice_1 choice_2)
- Next type default
- or type fixed default
- or type required
- or type implied
- Type gt to complete the attribute declaration
25Defining Attributes with Unique Values
- Type lt!ATTLIST tag
- Type attribute
- Type ID if you want the value to be unique
- Next type default
- or type fixed default
- or type required
- or type implied
- Type gt to complete the attribute declaration
26Referencing Attributes to Valid XML Names
- Type lt!ATTLIST tag
- Type attribute
- Type IDREF to define an attribute that will
contain a value that matches an other attributes
ID value - Type IDREFS for attributes that contain several
white-space-separated ID values - Type default, fixed default, required,
implied - Type gt to complete the attribute declaration
27Restricting Attributes to Valid XML Names
- To ensure attribute values follow the rules for
valid XML names - Type lt!ATTLIST tag
- Type attribute
- Type NMTOKEN if you want the value to be an XML
name - Or TYPE NMTOKENS if you want the value to be a
white space separated list of XML names
28Creating Shortcuts for Text
- In the DTD type lt!ENTITY
- Type abbreviation
- Type content
- Type gt to complete the declaration
- lt! ENTITY RDC Robert D. Cormiagt
29Using Shortcuts for Text
- In the XML document first type an
- Type abbreviation, where abbreviation is the
identifying name of your entity - (and matches the one used in the previous
example) - Type
- Ex. RDC would be Robert D. Cormia
30Shortcuts for Text in External Files
- Create the content for the entity in an external
file - In the XML document itself, add standaloneno
- In the DTD for the XML document type lt!ENTITY to
begin the entity definition - Type abbreviation
- Type SYSTEM entity.url
- Type gt
31Creating and UsingShortcuts for DTDs
- Create content for the entity
- add standalone no
- In the DTD for the XML document type lt!ENTITY
- Type
- Type abbreviation
- Type SYSTEM or PUBLIC
- Type SYSTEM entity.url
- Type gt
32Creating Entities for Unparsed Content
- In the DTD type lt!ENTITY
- Type abbreviation
- Type SYSTEM
- Type entity.url
- Type NDATA id
- Type gt
33Embedding Unparsed Content
- In the DTD define the element
- Type lt!ATTLIST element_name
- Type att_name
- Type ENTITY or ENTITIES
- Type the default value of the document
- Type gt to close the declaration
34Writing Filenames
- address_book_empty.dtd
- address_book_empty_dtd.xml
- address_book_mixed.dtd
- address_book_mixed_dtd.xml
- address_book_nested.dtd
- address_book_nested_dtd.xml
35Common DTD Mistakes
- Inconsistent data structure in your file
- Having an element appear more than once, or
omitting an element in a DTD - Not declaring an attribute for an element
- Changing an XML file but not the DTD
- Not checking your DTD with a validating parser
(EditML Pro / XML Spy etc.) - A Web browser provides weak validation
36Summary
- DTDs are contracts for XML documents
- They validate the data structure of a file
- DTDs are fairly straightforward to write
- Draw a picture of your XML application
- Clearly show the elements and attributes
- Declare all entities and entity references
- Use validating tools like EditML Pro and XML Spy
to validate XML using your DTD!