The xsd:complexContent type is used to extend or
restrict complex types in different ways. It is similar to deriving types from base
types in higher level programming languages such as C++ or Java. A common usage pattern
in the case of extension is to add additional elements to an existing sequence or choice
group. In this case, a new type is formed that contains all elements—those declared in
the base definition and those in the derived type. Also generated is a new type with the
name <baseType>_derivations
which is a choice of all of the different
derivations of the base type. This is used wherever the complex content base type is
referenced to allow any derivation of the type to be used in a message.
An example of this is as follows:
<xsd:complexType name="MyType"> <xsd:sequence> <xsd:element name="ElementOne" type="xsd:string"/> <xsd:element name="ElementTwo" type="xsd:int"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="MyExtendedType"> <xsd:complexContent> <xsd:extension base="MyType"> <xsd:sequence> <xsd:element name="ElementThree" type="xsd:string"/> <xsd:element name="ElementFour" type="xsd:int"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType>
The base type in this case is MyType
and it is extended to contain two
additional elements in MyExtendedType
. The resulting C type definitions for
MyType
, MyExtendedType
, and the special derivations type
are as follows:
typedef struct EXTERN MyType { const OSUTF8CHAR* elementOne; OSINT32 elementTwo; } MyType; typedef struct EXTERN MyExtendedType { const OSUTF8CHAR* elementOne; OSINT32 elementTwo; const OSUTF8CHAR* elementThree; OSINT32 elementFour; } MyExtendedType; #define T_MyType_derivations_myType 1 #define T_MyType_derivations_myExtendedType 2 typedef struct EXTERN MyType_derivations { int t; union { /* t = 1 */ MyType *myType; /* t = 2 */ MyExtendedType *myExtendedType; } u; } MyType_derivations;
The derivations type is a choice between the base type and all derivations of that base type. It will be used wherever the base type is referenced. This makes it possible to use an instance of the extended type in these places.
The case of restriction is handled in a similar fashion. In this case, instead of creating a new type with additional elements, a new type is created with all restrictions implemented. This type may be identical to the base type definition.