[xsd-users] Problem getting code to read data from files including using XInclude

Boris Kolpackov boris at codesynthesis.com
Thu Jul 16 04:03:56 EDT 2009


Hi Tim,

Tim Muirhead <Tim.Muirhead at tibra.com> writes:

> I've created an xsd schema that allows a choice between including a 
> section of config directly, or including it from an another file. 
> The example program (xmlval.cpp) then tries to read and print data.
> When I try and use an included file, it currently fails.  It knows
> that that the file has been included, but perhaps I missing a step
> (like explicitly parsing the included file?)

XInclude is handled by the underlying XML parser (Xerces-C++) and
is disabled by default. If you want to enable it, you will need
to set up your own XML-to-DOM and then DOM-to-object model stages.
For an example on how to do this, see, for instance, the multiroot
example in examples/cxx/tree. In your case you will need to add 
something like:

conf->setParameter(XMLUni::fgXercesDoXInclude, true);

in the dom-parse.cxx file. See the XInclude example in Xerces-C++
for more information.

The one big drawback of the XInclude implementation in Xerces-C++
is that you don't have control over the configuration of the
parser that is used to parser the included XML file. For example,
the schema validation setting is set to "auto" which means the
document will be validated if there is a schema available. There
is also no way to specify the schema location other than in the
included XML file.

If you want more control over the way the included file is parsed
then you will need to handle the inclusion yourself. You can do
it on the DOM level. However, it could be easier to do it on the
object model level. For this you will need to customize the type(s)
which need support for inclusion (TestComplexType in your case).
In the parsing constructor of this type you will need to detect
whether there is inclusion or the inline content. If it is the
inline content that you do nothing, just call the base c-tor and
it will handle everything. If there is inclusion then you load
the included fragment into a separate object model and then set
it as if it was the inline content. This way it will be complete
transparent to the users of your object model. 

For an example on how to customize the type and override its
parsing c-tor, see, for instance the wildcard example in the
examples/cxx/tree/custom/ directory. There is also the C++/Tree
Mapping Customization Guide:

http://wiki.codesynthesis.com/Tree/Customization_guide

Boris




More information about the xsd-users mailing list