[xsd-users] support for deviations from XML schema

Boris Kolpackov boris at codesynthesis.com
Thu Jan 29 09:32:41 EST 2009


Hi Azadeh,

Azadeh Omrani <a.omrani at gmail.com> writes:

> As far as I know for a program that uses XSD to work, it is necessary for
> the xml instance file(which is going to be parsed)  to comply with the
> corresponding xml schema.

The C++/Tree mapping uses XML Schema validation support from the
underlying XML parser (Xerces-C++). The generated code itself
performs minimal validation. Its only goal is to create a usable
object model. For example, if a required element is not present
in XML, then the object model will be unusable since if you try
to access the element's value, you will end up with access 
violation and there is no way to check whether the value is
present. As a result, the generated code catches this case.

So, if you disable full validation against XML Schema (the 
dont_validate flag), then the parsing will be very lax as long as
there are values for required attributes. In particular, the
generated parsing code does not care about element order as
long as there is no inheritance involved (if there is 
inheritance, then the base type will stop parsing as soon
as it detects first unknown element).

> assume that we can make static changes in the schema file (before
> compilation). but the xml file is out of reach to make changes in it.

Normally, if you want to make your schema as loose as possible, you
would change all required elements/attributes to be optional. You
can also add some wildcards (xs:any and xs:anyAttribute) to match
misspelled elements and attributes.


> 1-in the schema there is a complextype which has a sequence of ordered
> elements each having an unbounded cardinality, but in xml file those
> elements are not in order.

This will work as long as you disable XML Schema validation and 
there is no inheritance. You can also change your schema to allow
this:

<choice maxOccurs="unbounded">
  <element name="a" type="int"/>
  <element name="b" type="int"/>
  <element name="c" type="int"/>
</choice>

> 2-in the xml file, there are some elements which have typos in the 
> element or attribute names

See above. Plus you can add wildcards to match them (obviously there
is no magical solution to figure out the implied element name and 
parser it as such).

> 3- There are some required elements or attributes missing.

You will have to relax the schema for that.


> Is there any solution for each case of mentioned deviations? In a way that
> either the schema file can be changed to maintain some more flexibility ( in
> case 1) or the XSD xml parser can handle the situation and parse the rest of
> xml elements and ignore the typos or deviations without interrupting( in
> case 2 and 3)...?

If none of the above helps, you can always customize the generated
types and implement custom parsing. This way you can be as flexible
as you like.

Boris




More information about the xsd-users mailing list