XQuery/XQuery and XML Schema

Motivation
In learning any modelling language, it is helpful to see sample instances of any formal model. This is equally true when developing an XML Schema. XML Development tools like Oxygen and XML-Spy have tools to generate a random instance of a supplied XML schema, but it is useful to have a web service to do this. The service can also be used for test data generation.

The following is a test XML Schema file:

XQuery to convert Simple XML Schema into an Instance Document
We can use a recursive function to convert this XML Schema into an instance document. Here is an implementation of this function which covers some of the key XML Schema constructs:

This function uses the element construct to create a new XML instance tree based on the information in the XML Schema file and the typeswitch construct to select the appropriate processing of a given element type.

Type Instance Generation
Some helper functions are required to generate random instances of (some of ) the standard types.

Script
Here is a basic script which applies this function to the example schema. By default, the root of the generated instance is the first element in the schema or a named element if a root parameter is supplied.

Execute

To do

 * full set of xml types
 * mixed
 * restriction (only enumeration so far)
 * Group
 * AttributeGroup
 * problem with missing attributes in a complexType
 * hinting for distributions
 * namespaces
 * randomisation configuration