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:
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.
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:
A CLOB field in an Oracle database
The result of a query against a database that combines several records from different tables
A data structure created in memory by a Java program
A data stream created on the fly by a CGI program written in Perl
Some combination of several different files, each of which is embedded in another
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:
XML Declaration: This is an optional component that specifies the version of XML used and the character encoding scheme.
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.
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.
Attributes: These are used to provide additional information about an element, and are defined within the start tag.
Entities: These are placeholders for text or other content that can be referenced within the document.
Comments: These are used to add notes and explanations within the document and are enclosed within "".
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