XML

A DTD for Schemas

The schema specification provides a fairly complex DTD that can be used to define every possible schema. This DTD is designed to work with a wide range of possible schemas and to cover every possible condition. Here we'll work with a simplified DTD that presents a subset of the schema specification DTD. Any schema that conforms to the simplified DTD will also conform to the schema specification DTD.

A simplified DTD for schemas is shown below. (For the full DTD, visit http://www.w3.org/TR/xmlschema-1 to see the schema specification.)

  <!ENTITY % xs-datatypes PUBLIC 'datatypes' 'Datatypes.dtd'>
     %xs-datatypes;
  <!ELEMENT schema  ((include | import | annotation )*,
                     (element, simpleType, complexType,
                      attributeGroup, group, notation)*>
  <!ATTLIST schema  targetNamespace CDATA  #IMPLIED
                    version         CDATA  #IMPLIED
                    xmlns           CDATA  #REQUIRED
                    xmlns:dt        CDATA  #REQUIRED >
  <!ELEMENT element  ((annotation)?, (complexType | simpleType)?,
                      (unique | key | keyref)*)>
  <!ATTLIST element  type      CDATA  #IMPLIED
                     name      CDATA  #IMPLIED
                     ref       CDATA  #IMPLIED
                     minOccurs  (1 | 0 ) #IMPLIED
                     maxOccurs CDATA  #IMPLIED
                     id        ID     #IMPLIED
                     nullable (true | false ) 'false'
                     default CDATA #IMPLIED
                     fixed   CDATA #IMPLIED >
  <!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
            final
            block>
  <!ELEMENT group ((annotation)?, (all | choice | sequence)*)>
  <!ATTLIST group
              minOccurs   CDATA                '1'
              maxOccurs   CDATA                #IMPLIED
              order       (choice | seq | all) 'seq'
              name        CDATA                #IMPLIED
              ref         CDATA                #IMPLIED
              id          ID                   #IMPLIED>
  <!ELEMENT all ((annotation)?, (element | group | any |
                 choice | sequence)*)>
  <!ATTLIST all minOccurs CDATA #FIXED '1'
                maxOccurs CDATA #FIXED '1'
                id         ID   #IMPLIED>
  <!ELEMENT choice ((annotation)?, (element | group | any | choice |
                     sequence)*)>
  <!ATTLIST choice minOccurs CDATA '1'
                maxOccurs CDATA #IMPLIED
                id         ID   #IMPLIED>
  <!ELEMENT sequence ((annotation)?, (element | group | any |
                       choice | sequence)*)>
  <!ATTLIST sequence minOccurs CDATA '1'
                maxOccurs CDATA #IMPLIED
                id         ID   #IMPLIED>
  <!ELEMENT attribute  ((annotation)?, (simpleType)? )>
  <!ATTLIST attribute  type       CDATA  #IMPLIED
                       default    CDATA  #IMPLIED
                       fixed      CDATA  #IMPLIED
                       name       CDATA  #REQUIRED
                       minOccurs  (0|1)    '0'
                maxOccurs  (0|1)     '1' >
  <!ELEMENT attributeGroup ((annotation)?,
       (attribute | attributeGroup)*,
       (anyAttribute)?)>
  <!ELEMENT anyAttribute EMPTY>
  <!ATTLIST anyAttribute
            namespace    CDATA   '##any'>
  <!ELEMENT unique ((annotation)?, selector, (field)+)>
  <!ATTLIST unique name     CDATA       #REQUIRED
                     id       ID        #IMPLIED
                     uniqueAttrs>
  <!ELEMENT key    ((annotation)?, selector, (field)+)>
  <!ATTLIST key     name     CDATA       #REQUIRED
                     id       ID         #IMPLIED
                     keyAttrs>
  <!ELEMENT keyref ((annotation)?, selector, (field)+)>
  <!ATTLIST keyref  name     CDATA       #REQUIRED
                     id       ID         #IMPLIED
                     refer   CDATA       #REQUIRED>
  <!ELEMENT any EMPTY>
  <!ATTLIST any
              namespace       CDATA                  '##any'
              processContents (skip|lax|strict)      'strict'
              minOccurs       CDATA                  '1'
              maxOccurs       CDATA                  #IMPLIED>
  <!ELEMENT selector (#PCDATA)>
  <!ELEMENT field (#PCDATA)>
  <!ELEMENT include EMPTY>
  <!ATTLIST include schemaLocation CDATA #REQUIRED>
  <!ELEMENT import EMPTY>
  <!ATTLIST import namespace      CDATA #REQUIRED
                   schemaLocation CDATA #IMPLIED>

This DTD includes all the essential elements of a schema and also includes the data types' DTD. All the schema elements that will be defined in this chapter are listed. Notice that the elements you saw in XML Spy are now much more visible. The DTD uses a set of elements and attributes to define the structure of a schema document. The principal elements of a schema are simpleType, datatype, enumeration, schema, annotation, complexType, element, attribute, attributeGroup, and group. We've already looked at the first three elements; we'll examine the remaining elements next.

The schema Element

The schema element corresponds to the root element defined in a DTD. In a schema, all element elements are child elements of the schema root element. We will discuss the attributes of the schema element in the section on namespaces in this chapter.

Technically speaking, the DTD for a schema in the specification does not require that the schema element be the root element. The usual definition of a schema does have a schema element as the root, however.

The annotation Element

The annotation element is used to create comments within the complexType element. Comments are contained within one of two possible child elements of the annotation element: appinfo and documentation. The documentation element is used for human-readable comments. The appinfo elements are used for application-readable comments, as shown here:

  <annotation>
      <appinfo>
          The machine-readable comment goes here.
      </appinfo>
  </annotation>

Notice that the comment is content of the annotation element, which means that it is not enclosed in the usual comment symbols (<!--… -->). When the annotation element is an allowable child element for an element, it will always be the first child element.