DTD Basics   «Prev  Next»

Lesson 8DTD and XML file as separate files
Objective Create a DTD File to work with a Separate XML File.

Create a DTD File to work with a Separate XML File

Let us look at an example in which the DTD exists as a separate file. The name of the XML document is hello.xml, and the name of the DTD file is greeting.dtd:

Filename: hello.xml
Filename: hello.xml
  1. The standalone attribute has a value of "no" because the DTD information will not be included in the XML file.
  2. The <!DOCTYPE> declaration points to the external DTD file.

Filename: hello.xml
Separate DTD XML
Note that in this example, the word SYSTEM is used to specify that this DTD is located on the same system as the document itself. The word PUBLIC may also be specified in this position to indicate that the DTD is a publicly available document, and is followed by a reference to where that document can be found. Of course, greeting.dtd refers to the name of the file in which the DTD information can be found.


Filename: greeting.dtd 
<!ELEMENT greeting (#PCDATA)>

In this example, the XML file will be validated against the DTD. If the first file contained any other tags, this document would not be considered valid because only the greeting element has been defined by the DTD. The commonly accepted extension for the DTD file is .dtd . The next lesson shows you how to embed declarations within the XML file.

XML Documents

An XML document is made up of text. It is a sequence of characters with a fixed length that adheres to certain constraints. It may or may not be a file. For instance, an XML document could be any of the following:
  1. A CLOB field in an Oracle database
  2. The result of a query against a database that combines several records from different tables
  3. A data structure created in memory by a Java program
  4. A data stream created on the fly by a CGI program written in Perl
  5. Some combination of several different files, each of which is embedded in another
  6. One part of a larger file containing several XML documents
However, nothing essential is lost if you think of an XML document as a file, as long as you keep in the back of your mind that it might not really be a file on a hard drive. XML documents are made up of storage units called entities. Each entity contains a well-formed document fragment. This is a piece of text that meets all of XML's wellformedness rules except for the one about there being a single root element. The various entities that make up a document will be stored in different files, databases, and other locations. The parser combines them all to form the complete document.


What are the elements that make up an XML Document?

An XML document is composed of various components, including:
  1. XML Declaration: This is an optional component that specifies the version of XML used and the character encoding scheme.
  2. Document Type Declaration (DTD): This is another optional component that defines the structure of the XML document, including the names of the elements and attributes, and their relationships.
  3. Elements: These are the building blocks of an XML document and define the structure and content of the document. Elements consist of a start tag, an end tag, and the content between them.
  4. Attributes: These are used to provide additional information about an element, and are defined within the start tag.
  5. Entities: These are placeholders for text or other content that can be referenced within the document.
  6. Comments: These are used to add notes and explanations within the document and are enclosed within "".
  7. Processing Instructions: These are used to provide instructions to the application that processes the XML document and are enclosed within "<?" and "?>" delimiters.

All these components work together to define the structure and content of an XML document.

DTD Construction - Quiz

Click the Quiz link below to check your understanding of DTD construction.
DTD Construction - Quiz