[xsd-users] Groups and AttributeGroups

Mark Watson mark at digitalfountain.com
Fri Mar 9 12:34:52 EST 2007


Hi Boris,

Returning to this question. I have the templates approach working. However,
I have some 20-30 attributes/elements defined in 7 or 8 groups which then
appear in various combinations in 10 or so different types and the result is
hundreds and hundreds of lines of generated code. The templates approach
works, but it feels a little unmanageable at this scale.

What's the chance that some mechanism to factor out groups into common base
classes might make it into some future xsd release ? I think the multiple
inheritance might be a price worth paying for the simplification that would
come from representing the group structure of the schema within the C++
code. However, I didn't understand the other technical difficulty you
mentioned.

Best regards,

Mark Watson


On 2/8/07 12:34 PM, "Boris Kolpackov" <boris at codesynthesis.com> wrote:

> Hi Mark,
> 
> Mark Watson <mark at digitalfountain.com> writes:
> 
>> Is there a way to avoid duplication of code generated for groups and
>> attributeGroups when they are included in multiple elements ?
>> 
>> When an attribubuteGroup is used within two distinct elements, then separate
>> code seems to be generated in each element for the individual attributes in
>> the group. Is it possible to generate a class for the attribute group as a
>> whole and include this as a contained class (or super-class, if the
>> resulting multiple inheritance is not too much of a problem) within each
>> element which has the group ?
> 
> There is no such facility at the moment. Groups are really a syntactic
> sugar that allows you to "copy" the same content into several types
> while keeping things manageable. The content of a group a set of
> element or attributes and as such is mapped to a bunch of functions
> that do not really result in much object code (most of them are inline
> and one line long). There are also technical difficulties in factoring
> a group out as a base class since the type can also have a real base
> type which would require multiple virtual inheritance and non-trivial
> parsing delegation to handle properly.
> 
> 
>> This would make handling the resulting objects easier, as a single piece of
>> code could handle the common attributes across the various objects which use
>> those attributes.
> 
> You can achieve this with a function template. Suppose you have the
> following group:
> 
> <group name="g">
>   <sequence>
>     <element name="foo" type="int"/>
>     <element name="bar" type="string"/>
>   </sequence>
> </group>
> 
> You could then write the following function template:
> 
> template <typename X>
> void process_g (X& x)
> {
>   cout << "foo: " << x.foo () << endl
>        << "bar: " << x.bar () << endl;
> }
> 
> And call it on any instance of a type that embeds group g.
> 
> 
>> Alternatively, is there a different Schema construction instead of
>> attributeGroup which could achieve the same thing. I thought of defining a
>> type with the attributes and deriving the other types from that, but as I
>> have several attribute groups to play with and (unless I missed something)
>> XML does not support multiple inheritance (perhaps wisely).
> 
> The inheritance mechanism is your best bet. White XML Schema does not
> support multiple inheritance, you can always create a base that has
> two or more groups in it. The drawback of this approach is that you
> won't be able to have a single base for each group which you could
> handle generically. For example, if you have three groups, G1, G2,
> and G3, then you will need two base classes that contain, say, G1:
> B1={G1,G2} and B2={G1,G3}. As a result you will need write two
> functions to handle group G1: one that takes B1 and the other that
> takes B2. Overall, I think the function template approach is cleaner
> and less verbose.
> 
> 
> hth,
> -boris




More information about the xsd-users mailing list