Validation with Document Type Definitions (DTDs) - Getting to Know DTDs
(Page 2 of 4 )
Now that we know what document descriptions are and whether or not we should use one, we can begin learning about the first type, document type definitions or DTDs.
Before we begin however, it is important to know one thing: if you do decide to use a DTD to validate your XML, you must make sure your document is valid; certain processors will cease to operate if they encounter invalid documents. So think carefully before using them.
On the flip side, DTDs can help maintain structure and integrity within your data. A DTD gives definition to the elements and attributes that make up your document. Think of it as a form of grammar. The reason DTDs are optional in XML is because XML itself (when written properly) is well-formed, and processors can sometimes get the gist of what rules you are implying.
Below is a list of some terms you should know when dealing with DTDs.
Document Type Declaration (DOCTYPE) – This declaration comes after the XML declaration we learned before, and tells the processor where the DTD file is located.
It can look like this: <!DOCTYPE Root-Element SYSTEM “nameofthefile.dtd”>
Element Type Declaration – Used to give definition to an element type. An example would be: <!ELEMENT Name(#PCDATA)>
Attribute-list Declaration – Used to give definition to a datatype, default value, and name for each attribute associated with an element. An example of this would be: <!ATTLIST Element-Name Name Datatype Default>
Entity Declaration – Used to give definition to data referenced by an entity. An example of this would be: <!ENTITY Entity-Name “text”> . We will discuss entities more in a later article.
Notation Declaration – Gives association to a notation name with data that is used to find an interpreter program that is typically external, when XML processors cannot do so themselves. An example would be <!NOTATION NameSystem “externalID”>
XML Declaration – Informs the processor as to which version of XML you are using. An example would be <?xml version=”1.0” encoding=”UTF-8”?>
Next: Creating an XML Prolog >>
More XML Tutorials Articles
More By James Payne