[xsd-users] Including external schemas to reduce code duplication?

Boris Kolpackov boris at codesynthesis.com
Thu Feb 2 08:33:11 EST 2012


Hi Lyndon,

Lyndon <mail.lyndon at gmail.com> writes:

> My compiler is complaining about the duplicated code in the following
> situation:
> 
> Say I have three schema definitions: A.xsd, B.xsd, C.xsd.
> 
> A.xsd has some common element and type but no target namespace defined.
> 
> B.xsd has a target namespace called "Foobar" and includes A.xsd via
> <xs:include> (chameleon inclusion).
> 
> C.xsd also has a target namespace called "Foobar" and also includes A.xsd
> via <xs:include> (chameleon inclusion)
> .
> 
> When I generate code using xsd cxx-tree, it looks like the included
> elements and types from A.xsd are being generated and duplicated within my
> B.cxx and C.cxx.  In a previous version of xsd (2.3.1), the default
> behavior was to produce #include "A.hxx" in both B.hxx and C.xx, but it
> doesn't seem like this is the case anymore.

The new behavior is correct; in chameleon inclusion the included schema
assumes the namespace of the including schema which results in different
code being generated if the namespaces are different. In your case the
namespaces are the same but the XSD compiler has no way of knowing this.


> As a workaround, I used the --namespace-map option to map "Foobar" to two
> different C++ namespaces for B.hxx and C.xx respectively, but I was
> wondering if there was a way to reproduce the behavior from the previous
> version.

The --namespace-map approach is probably the best option if you cannot
or don't want to modify the schemas. Otherwise, you can add the target
namespace to A.xsd or include it in another schema that has the target
namespace and then include that schema in B.xsd and C.xsd. Using the
file-per-type mode (--file-per-type) is also an option, especially if
B.xsd and C.xsd are included into a root schema.

Boris



More information about the xsd-users mailing list