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