XML

The !ELEMENT Statement

Every element used in your XML documents has to be declared by using the <!ELEMENT> tag in the DTD. The format for declaring an element in a DTD is shown here:

  <!ELEMENT ElementName Rule>

The Rule component defines the rule for the content contained in the element. These rules define the logical structure of the XML document and can be used to check the document's validity. The rule can consist of a generic declaration and one or more elements, either grouped or unordered.

The Predefined Content Declarations

Three generic content declarations are predefined for XML DTDs: PCDATA, ANY, and EMPTY.

PCDATA

The PCDATA declaration can be used when the content within an element is only text-that is, when the content contains no child elements. Our sample document contains several such elements, including title, a, h1, and b. These elements can be declared as follows. (The pound sign identifies a special predefined name.)

  <!ELEMENT title (#PCDATA)>
  <!ELEMENT a (#PCDATA)>
  <!ELEMENT h1 (#PCDATA)>
  <!ELEMENT b (#PCDATA)>
NOTE
PCDATA is also valid with empty elements.

ANY

The ANY declaration can include both text content and child elements. The html element, for example, could use the ANY declaration as follows:

  <!ELEMENT html  ANY>

This ANY declaration would allow the body and head elements to be included in the html element in an XML document:

  <html><head/><body/></html>

The following XML would also be valid:

  <html>This is an HTML document.<head/><body/></html>

And this XML would be valid with the ANY declaration in our sample DTD:

  <html>This is an HTML document.<head/><body/><AnotherTag/></html>

The ANY declaration allows any content to be marked by the element tags, provided the content is well-formed XML. Although this flexibility might seem useful, it defeats the purpose of the DTD, which is to define the structure of the XML document so that the document can be validated. In brief, any element that uses ANY cannot be checked for validity, only for being well formed.

EMPTY

It is possible for an element to have no content-that is, no child elements or text. The img element is an example of this scenario. The following is its definition:

  <!ELEMENT img EMPTY>

The base, br, and basefont elements are also correctly declared using EMPTY in our sample DTD.

by BrainBellupdated
Advertisement: