The XSD ComplexType syntax allows for the specification of attributes that can be added to the start element tag for an XML instance of the type. XBinder handles attributes the same way it does normal elements. They are added as typed fields to the C struct or C++ class definition for the complex type.
The general mapping is as follows:
XSD type:
<xsd:complexType name="TypeName"> <xsd:group> ... </xsd:group> <xsd:attribute name="attr1" type="Type1"/> <xsd:attribute name="attr2" type="Type2"/> ... <xsd:attribute name="attrN" type="TypeN"/> </xsd:complexType>
Generated C code:
typedef struct TypeName { group type definition.. /* attributes */ Type1 attr1; Type2 attr2; ... TypeN attrN; } TypeName;
Generated C++ code:
class TypeName : public OSXSDComplexType { public: group type definition.. /* attributes */ Type1 attr1; Type2 attr2; ... TypeN attrN; ... } ;
In the definition above, group can be any content model group type (sequence, all, choice, or group). It is an optional item – it is possible to omit the group completely to form a type with empty content that only contains attributes.
Attributes are optional by default and are handled in the same way as optional elements. A bit is added to the optional bit mask at the beginning of the structure with the name attrPresent (where attr is the attribute name). This bit is set to true if the attribute is to be added to a message instance or false if it is to be omitted.
Attributes that contain a default or fixed value are handled by modifying the initialization and/or encode/decode functions. A default value will be handled by adding a statement to the initialization function for the type to set the attribute to the default value. The user can later override this value in order to change it in a message instance.
A fixed value also causes a statement to be added to the generated C initialization function or C++ constructor to set the attribute to the given fixed value. Unlike default value, it is not possible to override this value. The generated encode function contains hard-coded logic to ignore the value in the type variable and encode the fixed value. On the decode side, the incoming value will be checked to make sure it equals the fixed value. If not, an error will be flagged and the value set to the fixed value in the typed variable.