XML

The complexType Element

You can think of the complexType element as equivalent to a combination of the attributes and the child element list enclosed in parentheses in the element element declaration used in a DTD-essentially, it defines the child elements and attributes for an element element. The complexType element will define the element elements, attributes, or a combination that will be associated with an element element that has attributes or child elements. The simplified DTD in "A DTD for Schemas" declared the complexType element as follows:

  <!ELEMENT complexType  ( ((annotation)?, (%ordered;, %unordered;)*|
      (element | all | choice | sequence | group | any )*,
      (attribute | attributeGroup), anyAttribute)>
  <!ATTLIST complexType  content
            (mixed | empty | textOnly | elementOnly)  #REQUIRED
            name CDATA #REQUIRED
            derivedBy "(restriction|extension|reproduction)"> #IMPLIED
            base CDATA #IMPLIED
            id    ID   #IMPLIED >

The complexType element can contain three types of elements in the following order: comment, element, and attribute. The comment is located in the annotation element. Element information is usually defined using element or group elements. You can also use choice, sequence, any, or all elements to define the attributes within a complexType, as described later in this section. Attributes can be defined using the attribute, attributeGroup, or group elements.

The schema in "A DTD for Schemas" uses what is called an embedded complexType declaration-the declaration is embedded in the element declaration. The following fragment shows the complexType element embedded within the element element:

  <element name = "title">
      <complexType content = "textOnly">
         <attributeGroup ref = "i18n"/>
      </complexType>
  </element>

The complexType declarations have a scope, specifying where the data type can be seen in the document. Embedded datatype declarations can be seen only within the element in which they are embedded-that is, they have local scope. Thus, the title element can see the complexType element declared inside of it, but this complexType declaration is not visible from anywhere else in the document. You can also declare complexType elements outside of an element element. The complexType elements declared outside the element element are visible to the entire document and have document scope. You can reference a document scope element using the ref attribute. The document scope complexType elements will be discussed in detail later in this chapter.

NOTE
As we have mentioned, the schema element can contain element, simpleType, complexType, atttributeGroup, and group elements as child elements. When any of these elements are child elements of the schema element, they also have document scope.

The content attribute can be textOnly, mixed, elementOnly, or empty. If the content consists of only text and no elements, you can use textOnly. For both text and elements, you would use mixed. If the content is only elements, you would use elementOnly. When there is no content, you can use empty.

The ref attribute is used to reference document scope elements. The ref attribute can be used with attributeGroup, element, and group elements. When used with the attributeGroup element, it can reference only simpleType elements.

When an element element is included as the content of the complexType element, it represents a child element. Thus, the following code declares a child element of h1:

  <element name = "h1">
      <complexType content = "mixed">
          <element ref = "a"/>
           

Notice that the ref attribute is used to reference the name of the child element, in this case, a.

You can also use the minOccurs and maxOccurs attributes with the child element to specify its occurrence, as shown here:

  <element name = "h1">
      <complexType content = "mixed">
          <element ref = "a" minOccurs = "0" maxOccurs = "1"/>
  

We'll discuss the minOccurs and maxOccurs attributes in the next section. When you use an element that uses the ref attribute, it's as if the element that is being referenced is substituting the element that contains the ref attribute.

by BrainBellupdated
Advertisement: