Monday, 2 January 2012

XSD Types, Element Substitution Groups/References

  • The most obvious features offered in XSD that are not available in XML's native Document Type Definitions (DTDs) are namespace awareness and datatypes, that is, the ability to define element and attribute content as containing values such as integers and dates rather than arbitrary text
  • Types derived by restriction must repeat all the particle components (element declarations, model groups, and wildcards) of the base type definition that are to be included in the derived type. However, attribute declarations do not need to be repeated in the derived type definition. All derived type elements must be acceptable as base type elements
  • A complex type which extends another does so by having additional content model particles at the end of the other definition's content model, or by having additional attribute declarations, or both. Only appending is allowed (no other kinds of extensions)
  • Using derived types instead of base types: the xsi:type attribute which is part of the XML Schema instance namespace is used to identify exactly which derived type is intended
  • Type-based substitution is similar to a type cast in most programming languages, which many programmers want to avoid, and it adds the extra burden of having to sprinkle xsi:type throughout your instances. As an alternative, you can perform element-based substitution by providing substitution groups in the schema. A substitution group specifies a set of elements that can be swapped wherever a given element, also known as the head of the substitution group, is referenced (i.e. like an interface). An element indicates that it's a member of another element's substitution group through the substitutionGroup attribute
  • Declaring an element as abstract requires the use of a substitution group. Declaring a type as abstract requires the use of a type derived from it and identified by the xsi:type attribute
  • Attributes preventing type derivation and element substitution: "final" and "block" (element substitution only)
  • Most element declarations associate a name with a type definition. Sometimes it is preferable to use an existing element rather than declare a new element. In general, the value of the ref attribute must reference a global element

XSD Schema Components Details

Component Details
element and attribute declaration There is a distinction between "definitions" which create new types and "declarations" which enable elements and attributes with specific names and types to appear in document instances. Global element and attribute declarations cannot contain the "Ref" attribute, they must use the "type" attribute or be followed by an anonymous type definition. No occurence constraints (minOccurs, maxOccurs, use) may appear in the declarations of global elements and attributes. To represent a "null" value with an element that is present, e.g. when being sent to or from a relational database, set the "xsi:nil" attribute to "true"
simple type definition Attributes always have simple types. Simple types are built by restriction (facets), list or union. The 12 simple type restriction facets are:
  • maxInclusive
  • maxExclusive
  • minInclusive
  • minExclusive
  • totalDigits
  • fractionDigits
  • length
  • minLength
  • maxLength
  • pattern
  • enumeration
  • whiteSpace
complex type definition Elements that contain subelements/children or carry attributes are said to have complex types, whereas elements that contain strings/numbers/dates/... but do not contain any subelements are said to have simple types. Every complex type definition defines its own local (non-global) scope. Complex type content models:
  • simpleContent (no elements)
  • complexContent (some elements)
  • named group ("group")
  • un-named group ("choice", "sequence", "all")
model and attribute group definition Model and attribute group definitions do not participate in validation as such, but other components may be constructed in whole or part by reference to these groups. Thus, model and attribute group definitions provide a replacement for some uses of XML's parameter entity facility
identity-constraint definition Identity-constraint definition components provide for uniqueness and reference constraints with respect to the contents of multiple elements and attributes: unique, key (unique not null), keyref
notation declaration Reconstructs XML notation declarations. Notation declarations do not participate in validation as such. They are referenced in the course of validating strings as members of the NOTATION simple type
model groups Possible compositors:
  • choice
  • sequence
  • all
particles The minOccurs/maxOccurs attributes apply to "terms": elements, groups or wildcards. The default value for both the minOccurs and the maxOccurs attributes is 1. If both attributes are omitted, the term must appear exactly once
attribute uses Attributes can be declared with a "use" attribute to indicate whether the attribute is required, optional, or even prohibited (same usage as minOccurs/maxOccurs for terms). Default attribute values apply when attributes are missing while default element values apply when elements are empty. If an element does not appear in the instance document (i.e. it is missing), the schema processor does not provide the element at all, even if it has a default content
wildcards anyType represents the base type from which all simple and complex types are derived. An anyType type does not constrain its content in any way. anyType is the default type when none is specified. An "any" element specifies that any well-formed XML is permissible as content (processContents = skip/lax/strict)
annotation Annotations provide for human- and machine-targeted annotations of schema components ("documentation" resp. "appinfo")

XSD Schema Components Overview

There are 13 kinds of components, falling into 3 group:
  • primary components:
    • element declaration (named) - element
    • attribute declaration (named) - attribute
    • simple type definition (anonymous or named) - simpleType
    • complex type defintion (anonymous or named) - complexType
  • secondary components (all named):
    • group definition components (re-usable definitions):
      • model group definition - group
      • attribute group definition - attributeGroup
    • identity-constraint definitions - unique, key, keyref
    • notation declaration - notation
  • helper components (providing small parts of other components):
    • model group components (complex types content description):
      • model groups - all, choice, sequence
      • particles (element, group, any) - minOccurs, maxOccurs
      • attribute uses - required, default, fixed
      • wildcards - any, anyAttribute (and anyType)
    • annotations- documentation, appinfo