[xsd-users] Re: Expecting Sequence, Getting Optional

Boris Kolpackov boris at codesynthesis.com
Tue Oct 16 10:05:44 EDT 2012


Hi Andrew,

andrew.king2 at barclays.com <andrew.king2 at barclays.com> writes:

> I'm seeing something unusual with the code that was generated for
> the TradeIdentifier class. If you take a look at the two PNG files,
> you will see the XSD and the graphic for the TradeIdentifier as
> shown, which both seem to show that tradeId is a sequence. However,
> when you take a look at the code that was generated by CodeSynthesis
> in the attached header file, you will see that tradeId is an optional
> field, and not a sequence at all.

In XML Schema and C++ the term 'sequence' means quite different things.
In XML Schema sequence means that elements must appear in the specified
order (as opposed to any order for 'all' and "one of" for 'choice').
In C++ (as in most other programming languages), sequence means multiple
items. The C++/Tree mapping follows the C++ meaning in that the
"sequence cardinality" is used for elements that can appear more than
once.

Now, if you look at your schema, it has a lot of nested xs:sequence
compositors (some of which, BTW, are unnecessary) but in the end, none
of them specify maxOccurs greater than 1. Which means that their
contents can only appear once. There is also the choice compositor in
between, which means that the tradeId is only one of possible options.
That's why XSD makes it optional.

To summarize, for an element to be of the sequence cardinality it must
have maxOccurs greater than 1 or one of its containing compositors must
have maxOccurs greater than 1. You can read more about this in Section
2.8, "Mapping for Local Elements and Attributes" in the C++/Tree Mapping
User Manual:

http://www.codesynthesis.com/projects/xsd/documentation/cxx/tree/manual/#2.8

Boris



More information about the xsd-users mailing list