XML

Creating an unordered set of child elements

In addition to using commas to separate elements, you can use a vertical bar (|). The vertical bar separator indicates that one child element or the other child element but not both will be included within the element-in other words, one element or the other must be present. The preceding declaration can thus be rewritten as follows:

  <!ELEMENT p  (font | (img, br?) | a | ul | ol)+>

This declaration specifies that the p element can include a font child element, an (img, br?) child element, an a child element, a ul child element, or an ol child element, but only one of these elements. The plus sign (+) indicates that the element must contain one or more copies of one or several child elements. With this declaration, you can use child elements in any order, as many times as needed.

The additional markers (?, +, *) can be used to override the vertical bar (|), which limits the occurrences of the child element to one or none.

According to the new declaration, our XML code will be interpreted as follows:

  <p>
      <!--First group, containing single font element-->
      <font size=5>
          <b>Three Reasons to Shop Northwind Traders</b>
      </font>
      <!--Second group, containing the single child element ol-->
      <ol>
          <li>
              <a href="Best.htm">Best Prices</a>
          </li>
          <li>
              <a href="Quality.htm">Quality</a>
          </li>
          <li>
              <a href="Service.htm">Fast Service</a>
          </li>
      </ol>
      <!--Third group, containing a single child element img-->
      <img src="Northwind.jpg"></img>
  </p>

Suppose you also want to include text within the p element. To do this, you will need to add a PCDATA declaration to the group. You will have to use the vertical bar separator because you cannot use the PCDATA declaration if the child elements are separated by commas. You also cannot have a subgroup such as (img, br?) within a group that includes PCDATA. We can solve this problem by creating a new element named ImageLink that contains the subgroup and add it to the p element as follows:

  <!ELEMENT ImageLink  (img, br?)>
  <!ELEMENT p  (#PCDATA | font | ImageLink | a | ul | ol)+>

Web browsers that do not understand XML will ignore the ImageLink element. When you use PCDATA within a group of child elements, it must be listed first and must be preceded by a pound sign (#).

You can use the DTD to make certain sections of the document appear in a certain order and include a specific number of child elements (as was done with the html element). You can also create sections of the document that contain an unspecified number of child elements in any order. DTDs are extremely flexible and can enable you to develop a set of rules that matches your requirements.

The !ATTLIST Statement

Every element can have a set of attributes associated with it. The attributes for an element are defined in an !ATTLIST statement. The format for the !ATTLIST statement is shown here:

  <!ATTLIST ElementName AttributeDefinition>

ElementName is the name of the element to which these attributes belong.

AttributeDefinition consists of the following components:

  AttributeName AttributeType DefaultDeclaration

AttributeName is the name of the attribute. AttributeType refers to the data type of the attribute. DefaultDeclaration contains the default declaration section of the attribute definition.