[xsd-users] create object model with default values from schema?

Boris Kolpackov boris at codesynthesis.com
Fri Apr 8 11:54:12 EDT 2011


Hi Steffen,

Steffen Koepf <S.Koepf at ergo-elektronik.de> writes:

>   <xs:element name="config">
>   <xs:complexType>
>     <xs:sequence>
>       <xs:element name="SaveDate" type="xs:string" />
>       <xs:element name="FilamentU" type="xs:double" />
>       <xs:element name="GridU" type="xs:double" />
>       <xs:element name="AnodeU" type="xs:double" />
>       <xs:element name="TestLength" type="xs:int" />
>       <xs:element name="DescTubeSerial" type="xs:string" />
>       <xs:element name="DescNote" type="xs:string" />
>       <xs:element name="DescProcStep" type="xs:int" />
>     </xs:sequence>
>   </xs:complexType>
>   </xs:element>
> 
> All Elements are non-optional.
> I need an Object Model containing this elements already with
> Default values (0 vor numbers, "" for strings) 

These are not their default values in the XML Schema terms. If you
want them to have default values then you will need to set them
explicitly. And use attributes instead of elements, since in XML
Schema elements with default values have a different semantics.
For example:

<xs:complexType>
  <xs:attribute name="SaveDate" type="xs:string" default=""/>
  <xs:attribute name="FilamentU" type="xs:double" default="0.0"/>
  ...
</xs:complexType>

Alternatively, you could also use the --generate-default-ctor option.
However, while such a default c-tor will do the right thing for strings,
for doubles and ints it will leave the members uninitialized (since they
are mapped to fundamental C++ types). You can, however, make a small
change in libxsd to default-initialize (in C++ terms) fundamental types.
Let me know if you need more information on this method.

Boris



More information about the xsd-users mailing list