Validate Data with XML Schemas
Learn a validation approach that is easily adaptable to other
infrastructures for defining data types in a standard XML format
by Claude Duguay
February 2003 Issue
Validation specifications list permissible combinations of elements and attribute values to ensure the integrity of XML documents. Various validation strategies have been developed, such as Document Type Defintions (DTDs), XML Schema, RELAX, and TREX, to name a few. While each of these solutions is designed primarily for XML validation, there's no reason the same approach couldn't be applied to other validation requirements in your applications.
Most development projects could benefit from easily edited data type libraries. Ideally, we should be able to define these libraries using a standard schema definition language. The XML Schema specification, for example, supports 19 primitive data types, 25 derived data types, and 16 constraint facets that let you refine each data type definition. Numbers can be bounded, strings can be constrained to specific lengths, regular expressions can be applied, and so forth. XML Schema is very expressive and powerful enough to specify data type validation for all but the most unusual modern application.
Here we'll see how you can apply a Java library called the Multi-Schema XML Validator from Sun Microsystems and written by Kohsuke Kawaguchi. The Multi-Schema XML Validator is geared primary at XML validation and accomplishes its goals remarkably well, enabling validators of different types to be applied to any XML document, using the same infrastructure. While the Multi-Schema XML Validator infrastructure can parse multiple types of schemas, our focus will be on XML Schema, and specifically on the simpleType definitions.
When validating fields in a GUI application or constraining input values in a Web application, or even validating file content, there's little need for the XML Schema complex type, which, while more sophisticated, is geared at nested structures. We'll stick to simple data types in this discussion, but even simple types in this context can be surprisingly powerful. It's conceivable that complex types could be applied to relationships between fields, but we'll leave that for intrepid readers to explore. You'll find that simple types are more than enough for most applications.
Test on the Fly
Given that you can use XML Schema-
defined validators in any form-based application, it would be nice to have a way to develop schemas interactively and test them on the fly. To do this, I've written a simple GUI application that lets you edit the XML Schema, save it, and apply the changes to a trio of test components. The first component, a combo box, displays all of the known simple type validators for selection. The second and third components are a text field and button you can press to test the currently selected validator.
Back to top
|