[xsde-users] Overriding validation of empty elements

Boris Kolpackov boris at codesynthesis.com
Mon May 9 09:08:53 EDT 2011


Hi Klaus,

[I've CC'ed the xsde-users mailing list in case someone else will have 
a similar question in the future.]

Cinibulk, Klaus <klaus.cinibulk at siemens.com> writes:
 
> Consider the following simple schema definition (part of a sequence):
> 
> <xs:element name="SomeValue" type="xs:integer" minOccurs="0">
> 	<xs:annotation>
> 		<xs:documentation>some documentation</xs:documentation>
> 	</xs:annotation>
> </xs:element>
> 
> Due to some weird project requirements it would be desireable to allow
> the following XML fragment with schema validation turned ON:
> 
> <some_outer_tag>
> 	<SomeValue />
> </some_outer_tag>
> 
> Currently the parser throws an exception because <SomeValue /> is empty,
> which is perfectly correct according to the schema definition. Is it
> possible to tell the code generator / runtime library to allow such
> empty tags for integers or floats?

There is no easy way to achieve this. The best approach would probably
be to customize the integer (and float) parsers to detect empty content
and return some special values (e.g., 0, -1, or MAX_INT) in this case.

If you would like to go this route, then you will need to inherit from
the xml_schema::integer_pimpl class (see 
libxsde/xsde/cxx/parser/validating/integer.hxx) and override all its
callback functions. Here is how their new implementations might look:

struct custom_integer_pimpl: xml_schema::integer_pimpl
{
  virtual void
  _pre ()
  {
    v_.clear ();
    integer_pimpl::_pre ();
  }

  virtual void
  _characters (const ro_string& s)
  {
    v_.append (s.value (), s.size ());
  }

  virtual void
  _post ()
  {
    if (!v_.empty ())
    {
      integer_pimpl::_characters (v_);
      integer_pimpl::_post ();
    }
  }

  virtual long
  post_integer ()
  {
    if (!v_.empty ())
      return integer_pimpl::post_integer ();
    else
      return 0; // Return special value.
  }

private:
  std::string v_;
};

Then you will need to set this parser for the SomeValue element in the
aggregate. Something along these lines:

root_paggr root_p;
custom_integer_pimpl int_p;

catalog_p.container_p_.SomeValue_parser (int_p);

You can also encapsulate all this by creating a class derived from
root_paggr, adding the custom_integer_pimpl member to it, and then
setting the parser in its constructor.

Boris



More information about the xsde-users mailing list