An occurrence of xs:element specifies an element. It must have a "name" attribute or a "ref" attribute, but not both. If a "name" attribute appears, then a "type" attribute can appear. The value of a "type" attribute is the name of a type, either simple or complex; it specifies that the element will be of that type. The value of a "ref" attribute is the name of a global element; it indicates that the element being specified, presumably as part of a structure, has the same name and type as does the global element. In the absence of either a "type" or "ref" attribute, the element specification must contain an anonymous type (described later), or it will default to type "anyType" (also described later).
Schema elements come in two flavors, global and local. An xs:element element specifies both:
Typically, local element specifications are descendants of a global xs:element or xs:complexType. A global element can appear as the top-level element of an instance of the schema and must be defined with a "type" attribute.
A local element may have a minOccurs and/or maxOccurs attribute added to its xs:element specification to indicate the range of allowed occurrences. If minOccurs='0', the element is optional. You cannot restrict global elements in this manner. In effect, global elements, when used at the top level, always act as if they are specified with
which indicates that no upper bound restricts the number of occurrences. For local elements, the default for both minOccurs and maxOccurs is 1. Although you cannot apply these limits to global elements, you can apply them to an appearance, through the "ref" attribute mechanism, of a global element within a complexType.
Local elements are, in some sense, qualified by their ancestors up to the global element in which they occur. That is, different instances of the same local element name with different types can exist as long as they occur with different ancestors or within different global elements. This idea is similar to the way in which attributes can be local to their element.
Example 5-3 illustrates some of these features. It includes the following items:
Either "foo" or "owT" could appear at the top level of an instance of this schema.
Example 5-3 Elements schema
<xs:schema> <xs:element name="foo" type="bar"> <xs:complexType name="bar" mixed="true"> <xs:sequence> <xs:element name="intB" type="xs:integer" maxOccurs="42"> <xs:element name="DT" type="xs:dateTime" minOccurs="0"> </xs:sequence> </xs:complexType> <xs:element name="owT" type="charlie"> <xs:complexType name="charlie"> <xs:choice> <xs:element ref="foo"> <xs:element name="NM" type="NMTOKENS"> </xs:choice> </xs:complexType> </xs:schema>
Attributes are usually local, as they are specified inside an element schema. Nevertheless, you can have global attributes with a schema, which you specify with an "xs:attribute" element at the top level inside the root "xs:schema" element. You can use such global attributes in any element defined by the schema.
Local attributes do not have minOccurs or maxOccurs attributes on their specifications, but they can have a "use" attribute that can have "required" or "optional" or "prohibited" for its value. A "use" attribute cannot appear in a global attribute. All global attributes are, in effect, optional for all elements specified by the schema.
Values for an attribute or element can appear in a schema via the optional "default" and "fixed" attributes. Only one of these attributes may occur, however. A "fixed" attribute specifies the value or content that the attribute or element must have if it appears. Schemas treat "default" somewhat differently for attributes and elements. If an attribute is missing but the schema specifies a default value, then the attribute is inserted with that value. If the attribute is present but has a null value, a default does not disturb that value.
On the other hand, for elements, a missing element is not created with a default value. Only if the element is present but has null content does the default have any effect namely, it fills in the content of that element instance with the default value.
Example 5-4 illustrates some of these features of attributes in schemas. It defines three items:
The global attribute "atlas" can also appear on the Top element.
Example 5-4 Attributes schema
<xs:schema> <xs:attribute name="atlas" type="IDREF"/> <xs:element name="Top" type="TT"/> <xs:complexType name="TT"> <xs:attribute name="Middle" type="anyURI" use="optional"/> <xs:attribute name="Bottom" type="string" default="abc"/> </xs:complexType> </xs:schema>
The Schema Recommendation provides a mechanism for expressing groups of elements or groups of attributes so that they can be incorporated into types more easily. The xs:group element is somewhat like xs:complexType. It can appear at the top level and contain the same sort of content. It can then be instantiated inside a complexType by using an xs:group element with a "ref" attribute, thereby giving the name of the top-level xs:group. The effect is as if the top-level xs:group content had appeared in full when the "ref" was invoked.
The same sort of thing is possible with attributes using the xs:attributeGroup element. Such an element inside a complexType can invoke a top-level xs:attributeGroup by giving its name in a "ref" attribute.
Both xs:element and xs:attribute may have, as their content, a type structure without a "name" attribute. This "anonymous type" can be either a simpleType or, for elements, a complexType.
With schemas, the special type "anyType" is the root of all simpleTypes and complexTypes and is the least restricted type. Thus, if we declare an element
<xs:element name="foo" type="anyType"/>
it can be anything from empty to arbitrary mixed content.
The xs:any element may appear in a content model where we want additional flexibility. It enables you to restrict any valid XML content depending on the xs:any elements attributes. The processContents attribute can have "skip" or "lax" or "strict" as its value:
A "namespace" attribute consists of a list of allowable namespaces or classes of namespaces for "any" content, separated by white space. The list can include specific namespaces, "##any" to indicate that all namespaces are acceptable, "##local" to permit unqualified content, or "##other" to indicate that all namespaces except the targetNamespace (see below) are acceptable. "##targetNamespace" is also acceptable as a synonym for the target Namespace.