[xsd-users] XSD-tree Parsing Question

Boris Kolpackov boris at codesynthesis.com
Thu Sep 11 06:30:58 EDT 2008


Hi Karina,

Abayeva, Karina <karina.abayeva at rbccm.com> writes:

> I have a question regarding the flag to disable validation. I have a
> root element that defines several required child elements. Please see
> the example below for details:
>
>   <xs:complexType name="TestDataType">
>     <xs:sequence>
>       <xs:element name="Field1" type="xs:string" />
>       <xs:element name="Field2" type="xs:string" />
>     </xs:sequence>
>   </xs:complexType>
> 
> When the code is generated, parse function explicitly checks whether
> Field1_ and Field2_ member variables are present. If they are not, parse
> function will throw an expected element exception. Is there any way to
> make these presence checks configurable based on whether I passed in
> don't_validate flag?

The dont_validate flag controls XML Schema validation in the underlying
XML parser. Even when validation is disabled the generated code performs
a number of checks to make sure the created object model is valid. Even
if there was a way to disable these checks, the resulting object model
would most likely be unusable: there would be no way to check whether
the required element was set and if you try to access it you will get
a segfault.

> The only option that occurs to me is to create a wrapper function
> in a derived class that will catch expected element exception.

Catching the exception won't help much since the parsing won't
complete and the object model will be unusable. I see two ways
how you can resolve this:

1. If you can modify your schema (even if only for the purpose
   of code generation) then you can make those elements optional
   (add maxOccurs="0"). This is the easiest way.

2. The other way would be to customize the generated TestDataType
   type and perform custom parsing which ignores missing elements.
   You would probably also want to add a presence flag for each
   such element so that you can check whether you can access it.

   For more information on type customization, see the C++/Tree
   Mapping Customization Guide:

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

   There is also a bunch of examples in the examples/cxx/tree/custom/
   directory with the most relevant to your case being 'wildcard'.
   In your case, however, you will need to completely override
   the parsing code for this type (based on the wildcard example):

   data::
   data (const xercesc::DOMElement& e,
         xml_schema::flags f,
         xml_schema::container* c)
       : data_base (e, f | xml_schema::flags::base, c)
   {     
     ...
   }

   You can copy the body of this c-tor (as well as parse() function
   that it will call) from the generated code. In the parse() function
   you will need to change the checks for missing elements to set a
   flag instead of throwing an exception.

Boris




More information about the xsd-users mailing list