[xsd-users] Reusing schema components

Boris Kolpackov boris at codesynthesis.com
Fri Jan 15 08:36:01 EST 2010


Hi Joel,

Joel Stein <jstein at fiveringscapital.com> writes:

> <foo>
>   <a>1</a>
>   <b>2</b>
>   <c>3</c>
> </foo>
> 
> <bar>
>   <b>2</b>
>   <c>3</c>
>   <d>4</d>
> </bar>
> 
> I would love to take a bar and make a new foo and do foo.setB(bar.getB()) 
> and foo.setC(bar.getC()).  There seems to be 2 problems:
> 
> 1) (a small problem) the types don't match up in C++, even though they do 
>    in the schemas.
> 
> 2) In VC++9.0 (though it seems not in g++), I get a TON of linker errors 
>    (LNK2005 already defined) complaining about duplicate definitions of 
>     the shared components.

The best way to solve both of these problems (and also avoid code bloat) 
is to factor out common types (i.e., types for "b" and "c" elements) into 
a separate schema which can then be included into the schemas describing
"foo" and "bar".

If that's not an option (for example, because you cannot modify the 
schemas), then you can resolve the second problem by generating the
code for schemas foo and bar into different C++ namespaces, for example:

xsd cxx-tree --namespace-map http://example.com/my-ns=Foo foo.xsd
xsd cxx-tree --namespace-map http://example.com/my-ns=Bar bar.xsd

Use --namespace-map =Foo if your schemas don't have namespaces.

As for the first problem, in this case, you will need to "decompose"
the source object until built-in types and construct the target object
from these parts manually, for example (assuming CType is complex):

const Bar::CType& c = bar.getC ();
foo.setC (Foo::CType (c.getName (), c.getAge ()));

Boris



More information about the xsd-users mailing list