Validation
HydraExpress provides warnings at code generation time for errors and unsupported features in a schema document. At runtime, HydraExpress provides structural validation of the instance document and provides support for simple type validation.
Schema Document Validation
At code generation time, HydraExpress provides validation of the input schema through the Xerces Schema parser. Any error messages are passed directly to the standard output. Note that the Xerces parser expects the top-level schema element to have a schemaLocation attribute, and reports an error if one is not present.
Whatever the results of the validation, HydraExpress attempts to compile any well-formed XML Schema document, whether or not that document is a valid XML Schema. That is, the compiler treats warnings as diagnostic messages rather than errors. HydraExpress processes as much of the schema as possible, regardless of warnings. Therefore, providing an invalid schema may result in classes that do not compile.
Instance Document Validation
The purpose of an XML Schema is to define a set of constraints on the XML markup. Validation is the process of determining whether a document conforms to a specific set of constraints.
For an instance document, HydraExpress distinguishes three levels of validation. The lowest level of validation is that a document be well-formed.
• A well-formed document simply follows the characteristics of an XML document as defined in the XML 1.0 specification. No schema is needed for this determination.
• A structurally valid document is well-formed, and contains the element and attribute structure described by the schema.
• A schema valid document is structurally valid, and satisfies any global constraints. For example, the schema may require that no two elements have the same value for a serialNumber attribute.
At runtime, HydraExpress provides structural validation when marshaling or unmarshaling a document.
When unmarshaling a document into an object, the unmarshal method throws an exception when the XML document is incorrect. The function ignores minor defects. For example, the method silently ignores attributes not mentioned in the schema, but throws an exception if a required attribute is not present.
When marshaling an XML document from an object, the marshal function produces a structurally valid XML document. If the data in the object conforms to the schema, HydraExpress produces a schema valid document.
Each class that HydraExpress generates contains an isValid() method. This method can be used to validate simple type data associated with the class. Currently isValid() validates all simple type data; however, for patterns and enumerations, the method validates only string types. In addition, isValid() checks user defined facets, but not the data’s string base type.
The
isValid() method returns a
bool value of
true if no errors are detected, or
false if any data values are found to violate restrictions or facets expressed in the schema. For a return value of
false, the errors are stored in a Standard C++ Library
vector of
rwsf::XmlSchemaException objects. If you use the overload of
isValid() that passes in an
rwsf::XmlValidator instance, you can use the validator’s
getErrors() method to obtain the
vector object, then iterate through the vector to obtain information on the errors.
rwsf::XmlSchemaException inherits from
rwsf::Exception, so you can use the
what() method to obtain the string that describes the error. For an example of how to check for errors when using
isValid(), see
“Validating the Instance Document.”