[xsd-users] Groups and AttributeGroups

Boris Kolpackov boris at codesynthesis.com
Thu Feb 8 15:34:31 EST 2007


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20070208/5dcf2590/attachment.pgp


More information about the xsd-users mailing list