XML Document Type Declaration

Table of Contents

Toggle

In the previous post, you learned about XML declarations in detail, now we look into the document type declarations. The document type declarations are optional entries that includes DTD for validating your documents, root element, and any entities that you want to declare.

Syntax

The syntax for XML document type definition is given below.

Advertisements
Figure 1 – XML Document type declaration syntax

The document type declaration starts with <!DOCTYPE declaration, and next is:

  1. name of the root element
  2. SYSTEM means the local directory where you can find current XML document.
  3. DTD file enclosed in quotes.
  4. The two square braces for any internal entities or elements declaration.

The exteranl DTD is called for validity check of the document. The XML parser will match the document instance with the content of the DTD document model and does the validity check.

This is purely optional, and it checks for the elements pattern and required data in the document.

The next is SYSTEM that map to the current directory of the XML and expect to find the DTD file there, if you have located the file elsewhere, you must provide a URI path to the DTD file. The name of the file is enclosed in quotes.

Advertisements

Here is an example of document type declarations.

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE cat SYSTEM "cats.dtd"
[
 <!ENTITY breed "Bengal cat">
 <!ENTITY color "Black">
]> 

It is possible that you may have some internal declaration where you want to change the existing DTD information of the DTD fil(external), basically, you are redefining it. Usually, it contains the entities declarations and other parts of the document. It is only place to declare something within the document.

Summary

In this article, you have learned about the XML document type declaration within the prolog, that help define some document type definition file and internal entities, purely optional. If you declare the DTD then parser will try to check the validity against the DTD file.

Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version