XQuery/XML Schema to Instance

Motivation
In order to generate a sample XML instance from an XML Schema file (XSD), this is useful for example if you would like to dynamically generate blank XForms instances for creating new documents from a schema.

Method
[Note: See also the article XQuery/XQuery and XML Schema which has the same objective. If the author would like to contact me, I'm trying to get this code working to compare with the code I developed. ChrisWallace (talk) 15:09, 13 May 2009 (UTC)ChrisWallace]

Create an xquery function that reads in a URI to an XML Schema file (.xsd), along with a set of display parameters, and generates a sample XML instance. These parameters are:


 * 1) $schemaURI = the location of the .xsd file (e.g. db/cms/schemas/MySchema.xsd)
 * 2) $rootElementName = the root element for the sample XML file you wish to generate (i.e. doesn't have to be the root of the whole schema)
 * 3) $maxOccurances = for elements with a maxOccurs attribute greater than one, how many times should the element be repeated in the sample instance?
 * 4) $optionalElements = Should optional elements (i.e. minOccurs="0") be included? 'true' or 'false'
 * 5) $optionalAttributes = Should optional attributes (i.e. use="optional") be included? 'true' or 'false'
 * 6) $choiceStrategy = Where there is a choice between elements or groups of elements, should the sample include a random selection from the choices, or simply use the first choice? 'random' or 'first'

Call the function with the following: