11.2. XML Syntax

XML follows guidelines we have already set forth for XHTML:

  • Element and attribute names are case sensitive.

  • All elements must be properly closed.

  • Elements must be properly nested, not overlapping.

  • All attributes must have values.

  • All attribute values must be quoted.

Within documents, the structure is similar to that of HTML, where element tags are used to encapsulate content that may itself contain tag-delimited content.

The following sections outline the particular syntax of the various XML document elements.

11.2.1. XML Declaration and DOCTYPE

Each XML document should begin with an XML declaration similar to the following:

<?xml version="1.0" encoding="UTF-8"?>

The declaration is <?xml?>, with version and encoding attributes. The version attribute specifies the version of XML the document uses, and the encoding attribute specifies the character encoding used within the document's content.

As with other markup languages, XML supports document type definitions (DTDs), which specify the rules used for the elements within documents using the DTD. Applications can then use the DTD to check the document's syntax. An XML document's DTD declaration resembles that of an XHTML document, specifying a SYSTEM or PUBLIC definition. For example, the following DTD is used for OpenOffice documents:

<!DOCTYPE office:document-content PUBLIC
   "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "office.dtd">

The following is an example of an XHTML document's DTD definition:

<!DOCTYPE html ...

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.