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.