[xsd-users] Data validation

Boris Kolpackov boris at codesynthesis.com
Fri Jan 27 08:15:35 EST 2006


David,

Moss, David R (SELEX Comms) (UK Christchurch) <david.r.moss at selex-comm.com> writes:

> If in a schema I have a restricted data type defined as:
>
> <xsd:simpleType name="myInt">
>    <xsd:restriction base="xsd:unsignedInt">
>       <xsd:minInclusive value="100" />
>       <xsd:maxInclusive value="200" />
>    </xsd:restriction>
> </xsd:simpleType>
>
> <xsd:element name="num" type="myInt"/>
>
> then on parsing an xml file with an element of that type outside of that
> range will throw an exception as expected:
>
> <num>999</num>
>
> Datatype error: Type:InvalidDatatypeFacetException, Message:Value '999'
> must be less than or equal to MaxInclusive '200' .
>
>
> However, if I want to modify / create some data programmatically no such
> validation occurs. The following compiles, runs and serializes back to
> an xml file without any problem:
>
> myInt i(9999999);
>
> Is there any way to use the underlying validation used when an xml file
> is parsed to check the validity of objects instantiated in this way?

Validation of in-memory tree is on our TODO list. When it is implemented
the above code will simply throw an exception. There will also be the
validate() function which you will be able to call on the root of the
tree and it will check constraints that cannot be validated right away,
for example, minOccurs constraint in xsd:element definition.

While this facility is not available, the next thing to try would be
to serialize the document to DOM and validate that. Unfortunately,
Xerces-C++ does not support DOM validation (hopefully they will add
it in 3.0). So, for now, the only way to do what you want is to serialize
the tree all the way to XML (e.g, in a memory buffer) and re-parse it
to detect errors. I agree this is backwards.

hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20060127/dce7953c/attachment.pgp


More information about the xsd-users mailing list