About Me

Friday, 13 April 2012

XML Prolog

XML Prolog

The prolog is an optional component of the XML document. If included, the prolog must be appear before the root element. A prolog consists of two parts: the XML declaration and the Document Type Declaration (DTD). Depending on your needs, you can choose to include both, either, or neither of these items in your XML document. The DTD is most oftenly used, so we will discuss its use and purpose first.

XML Document Type Declaration (DTD)

The Document Type Declaration is a file that contains the necessary rules that the XML code in this file must follow. You may think of the DTD as the grammar that the XML document must abide by to be a valid XML file.
In later lessons we will discuss how to create your own DTD, which will allow you to make your own XML rules. For now we will simply show you how to reference an existing DTD file.

Referencing an External DTD

There are two type declarations that may be used to reference an external DTD: PUBLIC and SYSTEM. When creating an XML document under the rules of a publicly distributed DTD, use PUBLIC. Otherwise, use the SYSTEM type declaration.
The example below shows a prolog that would be used for an HTML document that is using an XML prolog. The DTD is publicly available thanks to the W3C.

XML Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Let's take a look at each piece of this external DTD reference.
  • !DOCTYPE - Tell the XML processor that this piece of code defines the Document Type Definition
  • html - Specifies the root element of the XML document. Here our example is an HTML file, which has <html> as the root element.
  • PUBLIC - Specifies that this a publicly available DTD.
  • "-//W3C//D..." - The definition of the public document. Describing this is beyond the scope of this tutorial.
  • "http://www.w3.org/..." - The physical location of the DTD. Notice how this DTD resides on w3's servers.

The XML Declaration

This is where you define what version of XML you are using. The current version is 1.0, which it has been for quite some time now. Check out the most recent XML version at w3.org.

XML Code:

<?xml version="1.0"?>
The declaration is not absolutely necessary, but should be included anyways.

XML & DTD Declaration Together

If you were to include both an XML declaration and a DTD declaration on an XML document then you would place the XML declaration first, followed by the DTD declaration. We have done just that in the example below.

XML Code:

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Be sure to include the optional prolog when you want to run your document through an XML processor to check for validity. If you have a valid DTD, simply use the free markup validation tool at W3C.

0 comments:

Post a Comment