[xsd-users] Basic questions

Boris Kolpackov boris at codesynthesis.com
Fri Nov 9 07:43:43 EST 2007


Hi Bruno,

bruno.marotta at fortis.com <bruno.marotta at fortis.com> writes:

> The thing is that main xsd doesn't define its type:
>
> 	<xs:element name="CDOEvaluator"/>

This declaration is equivalent to:

<xs:element name="CDOEvaluator" type="xs:anyType"/>

Which means CDOEvaluator element can contain pretty much anything
and no validation will be done on its content. This most likely is
not something that the author of the schema intended, but if it
is then read on.


> The xml I have to generate is something like this:
>
> <CDOEvaluator>
> 	<InputForSDR>
> ...
>
> I can create my CDOEvaluator and InputForSDR node like this:
>
> 	xml_schema::type cdoEvaluator
> 	...
> 	InputForSDR inputForSDR(p);
>
> But how can I add the inputForSDR to the cdoEvaluator?


You cannot since xml_schema::type (mapping for xsd:anyType) does not
know anything about InputForSDR or any other content.

If there is global element InputForSDR defined in one of your schemas,
then you can write the <CDOEvaluator> openning tag and </CDOEvaluator>
closing tag yourself and then serialize InputForSDR using the XSD-generated
object model (you will need to pass the no_xml_declaration flag to the
serializer function). For an example that does pretty much exactly this
see examples/cxx/tree/streaming/ in the XSD distribution.

If you do not have the InputForSDR element (and cannot change the
schema to define one) then things are bit more complicated. You will
need to (1) create a DOMDocument with the CDOEvaluator as root element,
(2) add the InputForSDR element under it, then (3) serialize the
InputForSDR instance (inputForSDR) into InputForSDR element, and,
finally, (4) serialize the DOM document to XML.

Steps 1, 2, and 4 are covered in the C++/Tree Mapping FAQ, Section 3:

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

Step 3 is actually quite easy:

DOMElement* inputForSDRElement = ...;

*inputForSDRElement << inputForSDR;


Let me know if you run into any problems.

Boris




More information about the xsd-users mailing list