Generate Sample XML/JSON/YAML File
The Generate Sample XML/JSON File command is enabled in Text View, Grid View, and Schema View, and generates an XML, JSON instance, or YAML document based on the currently active schema file:
•If the currently active file is a DTD or XML Schema, then an XML instance file can be generated from it.
•If the currently active file is a JSON schema, then a JSON instance document or a YAML document can be generated from it.
The generated file is opened in a new window in XMLSpy, from where you can save it to file. The setting for generating (i) XML files and (ii) JSON and YAML files are described below.
Generating sample XML files
With a DTD or XML Schema active, you can generate a sample XML instance based on the schema. On clicking the Generate Sample XML/JSON File command, the Generate Sample XML File dialog (screenshot below) appears, in which you can specify the options for the sample generation.
Elements of choice groups
A choice group is a group of elements from which one may be used. For example, if an element called items is defined as having a choice group consisting of the three elements: cd, dvd, book, then items can validly have any one of these three elements as a child element (with a maximum number of occurrences as specified in that element's maxOccurs attribute).
In the Generate Sample XML File dialog, you can select whether (i) the first branch (element) of the choice group, (ii) all branches, or (iii) the branch with the smallest number of descendant elements is generated. Note that the All branches selection could generate an invalid document since only one branch from a choice group is allowed.
If any of the choice groups's branches are repeatable (that is, it has a maxOccurs value of greater than 1), then you specify, in the first text box of the dialog, how many of the repeatable elements to generate, up to a maximum of 99. If the maxOccurs attribute of the choice group is defined as unbounded or as a large number and All branches is selected in the Generate Sample XML File dialog, then the maxOccurs of the choice group is also limited by the number of repeatable elements you specify in the first text box of the dialog.
Generate non-mandatory elements
Activating this option generates both the mandatory and non-mandatory elements defined in the schema. If activated, you can specify the level of nesting that you want. A greater nesting depth enables non-mandatory elements to be generated up to the nesting level you specify.
Generate non-mandatory attributes
Activating this option generates both the mandatory and non-mandatory attributes defined in the schema.
Generate X elements if marked repeatable in Schema/DTD
Activating this option generates the number of repeatable elements you enter in the text box. This applies to all elements, including those in choice groups.
Fill elements and attributes with data
Activating this option inserts the datatype values of the respective elements and attributes. For example if an element is defined as being pf datatype string, then the element is given a dummy value of string.
Nillable elements and abstract types
The contents of nillable elements can be treated as non-mandatory, and elements with an abstract type can use a non-abstract type for its xsi:type attribute.
Schema assignment for the generated XML file
The schema used to generate the XML file can be assigned to the generated XML file with a relative or absolute path.
Use manually added sample values if available
If the schema component has sample values assigned to it, then these will be used as the value or content of that component. For individual components, sample values are assigned in the Facets Entry Helper, in the Samples tab. Which value from the available sample values is selected for a single file generation can be specified:
•A random selection.
•Each sample value in turn for each instance of the component. For each file generation, the cycle starts anew.
•The first value always.
Root element
If the schema contains more than one global element, these are listed, and the root element required for the sample XML file can be selected from the list.
Namespaces
Click the Namespaces button to open the Edit Namespaces dialog (screenshot below), The namespaces that are defined in the schema, plus any standard XML Schema namespaces that are required in the sample XML file, will appear in this dialog.
You can edit the following:
•The namespace prefix that is bound to any of the document's namespaces. The namespace prefixes that are set in this dialog will be used (in the generated XML file) to prefix nodes that are in the corresponding namespace. For example, the screenshot indicates that nodes in the http://www.xmlspy.com/schemas/textstate namespace will be prefixed with ts: in the sample file .
•You can set one of the document's namespaces to be the default namespace (xmlns=) by selecting, in the xmlns= combo box, the namespace that you want. Nodes in the namespace that is selected as the default namespace will then be generated without a namespace prefix.
Generating sample JSON/YAML files
With a JSON schema active, you can generate a sample JSON or YAML instance file that is based on the JSON schema. On clicking the command, the Generate Sample JSON/YAML File dialog (screenshot below) appears. Choose the format of the JSON/YAML file to generate and specify the options.
You can choose whether to generate non-mandatory object properties, non-mandatory pattern properties, the length of arrays, the repetitions of recursive definitions, and the maximum number of optional values. You can also specify whether the active JSON schema should be automatically assigned to the generated JSON or YAML sample file. If the JSON schema is assigned, then it will be added as the validation schema to the JSON tab of the Info window.