XML

complexType Elements with Document Scope

Now that we've covered group and attributeGroup elements, we can examine document scope complexType elements in more detail. If you have a grouping of attributes and elements that will be used by more than one element element, you can create a document scope complexType element. You declare the document scope complexType element exactly as you declare the embedded complexType element, except the declaration will include the name attribute and will not be within the content of an element element-that is, it will be outside an element element declaration. Thus, it will be declared as a child element of the schema element. For example, all the h elements share a common set of child elements and attributes. You could declare a global complexType element and use it as shown here:

  <schema>
      <complexType name= "standardcontent" content = "mixed">
          <element ref = "a"/>
          <element ref = "br"/>
          <element ref = "span"/>
          <element ref = "img"/>
          <element ref = "tt"/>
          <element ref = "i"/>
          <element ref = "b"/>
          <element ref = "big"/>
          <element ref = "small"/>
          <element ref = "em"/>
          <element ref = "strong"/>
          <element ref = "q"/>
          <element ref = "sub"/>
          <element ref = "sup"/>
          <element ref = "input"/>
          <element ref = "select"/>
          <element ref = "textarea"/>
          <element ref = "label"/>
          <element ref = "button"/>
          <element ref = "script"/>
          <element ref = "noscript"/>
          <attribute name = "align" type = "string"/>
          <attributeGroup ref = "attrs"/>
      </complexType>
      <element name= "h1" type ="standardcontent"/>
      <element name= "h2" type ="standardcontent"/>
      
  </schema>

You can also extend a complexType element using the base attribute. For example, the li element uses all the preceding content and several other elements. You can extend the complexType element example as follows:

  <complexType name = "licontent" base = "standardcontent"
      derivedby = "extension">
      <element ref = "p"/>
      <element ref = "h1"/>
      <element ref = "h2"/>
      <element ref = "h3"/>
      <element ref = "h4"/>
      <element ref = "h5"/>
      <element ref = "h6"/>
      <element ref = "div"/>
      <element ref = "ul"/>
      <element ref = "ol"/>
      <element ref = "hr"/>
      <element ref = "blockquote"/>
      <element ref = "fieldset"/>
      <element ref = "table"/>
      <element ref = "form"/>
  </complexType>
  <element name= "li" type ="licontent"/>
  

To extend a complexType element, you need to use the base and derivedBy attributes of the complexType element. The base attribute identifies the source of the element and can be either #all, a single element, or a space-separated list. The derivedBy attribute can be set to restriction, extension, or reproduction. When you are adding elements or attributes to a complexType element, the derivedBy attribute should be set to extension.

The restriction value for the derivedBy attribute allows you to add restrictions to the element elements included within the complexType element. For element elements included in the original complexType element, you can restrict the number of occurrences of an element or replace a wildcard with one or more elements. For example, if you wanted to restrict the a element to one or more occurrences and remove the br element in a new complexType element, based on the standardcontent type defined in the preceding example, you could write the following code:

  <type name = "licontent" base = "standardcontent"
      derivedby = "restriction">
      <element name = "a"  minOccurs = "1"/>
      <element name ="br" maxOccurs ="0"/>
  </type>

For attributes, you can add or fix defaults or restrict the attribute's simple data type definition.

If you set the derivedBy attribute to reproduction, the new element is identical to the type it is derived from. Essentially, reproduction indicates neither restriction nor extension.

If the value for the final attribute for the complexType element is not empty, the complexType cannot be extended, restricted, or reproduced. A complexType that is derived by extension, restriction, or reproduction also cannot be extended, restricted, or reproduced. The block attribute allows you to block extension, restriction, or reproduction. If you set the block attribute to restriction, the complexType element cannot be used to create a new complex type by restriction.