[xsd-users] Using --file-per-type and importing

Boris Kolpackov boris at codesynthesis.com
Fri Apr 13 14:05:13 EDT 2012


Hi Doug,

LINHARDT DOUGLAS-VJKP74 <douglinhardt at motorolasolutions.com> writes:

> I want to be able to use XML Schema to define a set of messages to be sent
> around my system. I defined a base Message type (in the BaseMessageSchema
> namespace) in BaseMessage.xsd, and a set of derived messages (in the
> DerivedMessageSchema namespace) in DerivedMessages.xsd, importing
> BaseMessage.xsd.
> 
> When I compile DerivedMessages.xsd without --file-per-type, I generate only
> DerivedMessages.*.

The default mode is file-per-schema and you need to compile each schema
file, including included and imported ones (except for chameleon inclusion).


> If I compile DerivedMessages.xsd with the --file-per-type, I get the
> DerivedMessages* and files for all of the types defined within
> DerivedMessages.xsd. This I expected.  However, I also got BaseMessage.* and
> Message.* files. This I did not expect. I also tried putting the derived
> messages in the same namespace as the base Message type and including
> BaseMessage.xsd, but I got the same result.

In the file-per-type mode XSD generates a set of C++ files for each type
defined in the schema, including from included and imported schema. It
also generates C++ files corresponding to the schema files which generally
contain two things: #include directives for all the files corresponding
to the type defined by this schema and parsing/serialization functions
corresponding to the global elements defined in this schema.

If you don't need parsing/serialization functions, then you can ignore
these second kind of files (for the next release of XSD we also have
added the --fat-type-file option which triggers the generation of code
corresponding to global elements into type files instead of schema
files; if you are interested I can build you a pre-release with this
feature).

> I prefer to have files for each of the types-I think it's easier for a
> developers looking for a method name for a specific class. However, the
> derived message types will be spread out all over the file system, and I
> don't want to re-generate the BaseMessage.*and Message.* files in every
> directory where derived files are generated.

There is a way to make all the derived messages use one set of Message.*
files with the --include-regex option (they will still be generated
when you compile the derived schemas, but then you can ignore/remove
them).

The more straightforward approach, however, would be to define each
derived message in a separate schema and then use the file-per-schema
mode. This way your schema and your C++ files will have the same
structure.


> Why is the behavior of file-per-type and file-per-schema different here? I
> get what I am looking for with file-per-schema, but not with file-per-type.
> 
> File-per-schema is not a deal-breaker, but it would be nice to generate
> file-per-type, excluding imported and included types.

The file-per-type mode's primary purpose is to handle certain schemas that
have inclusion cycles involving inheritance. It is impossible to generate
correct code for such schemas without knowing the complete set of types.

Boris



More information about the xsd-users mailing list