Skip to main content

Generating Classes from XML Schemas

This page describes how to generate a set of XML-enabled classes that correspond to the types defined in an XML schema.

Note:

The XML declaration of any XML document that you use should indicate the character encoding of that document, and the document should be encoded as declared. If the character encoding is not declared, InterSystems IRIS uses the defaults described in Character Encoding of Input and Output. If these defaults are not correct, modify the XML declaration so that it specifies the character set actually used.

Generating the Classes

To generate XML-enabled classes that correspond to the types defined in an XML schema, use the Process() method of the %XML.Utils.SchemaReaderOpens in a new tab class. To use this method:

  1. Create an instance of %XML.Utils.SchemaReaderOpens in a new tab.

  2. Optionally set properties of the instance to control its behavior. In particular, you may want to set these properties (most of which are boolean):

    • KeepEmptyClasses, which specifies whether to keep unused classes that have no properties. If this property is 1, such classes are not removed at the end of the generation process; otherwise, they are removed.

    • NoArrayProperties, which controls to generate array properties. If this property is 1, the system does not generate array properties but instead generates another form. See Creation of Array Properties in Details of the Generated Classes.

    • GenerateXMLNIL, which controls to specify the XMLNIL property parameter for applicable properties in the generated classes.

      This option applies to each property that corresponds to an XML element that is specified with nillable="true". If this property is 1, the system adds XMLNIL=1 to the property definition. Otherwise, it does not add this parameter. For details on this parameter, see Handling Empty Strings and Null Values.

    • GenerateXMLNILNOOBJECT, which controls whether to specify the XMLNILNOOBJECT property parameter for applicable properties in the generated classes.

      This option applies to each property that corresponds to an XML element that is specified with nillable="true". If this property is 1, the system adds XMLNILNOOBJECT=1 to the property definition. Otherwise, it does not add this parameter. For details on this parameter, see Handling Empty Strings and Null Values.

    • CompileClasses, which controls whether to automatically compile the generated classes.

    • MakeNamespace, which controls whether to specify the NAMESPACE parameter. In this case, NAMESPACE is set to the value of the targetNamespace in the schema.

      If this property is 1, NAMESPACE is not specified.

      It is recommended to specify this as 1 in all cases, because every XML-enabled class should be assigned to an XML namespace.

    • MakePersistent, which controls whether the generated classes should extend %PersistentOpens in a new tab.

    • MakeRelationships, IndexRelationship and ParentChildRelationship, which controls the form of connections between persistent classes. These properties apply when the schema has a <complexType> A that consists of a <sequence> of another <complexType> B. When the code generates a persistent class containing property A, there are three possible forms for this property. It can be defined as a list of objects, as a one-to-many relationship (the default), or as a parent-child relationship. The permutations are summarized in the following table:

      MakeRelationships IndexRelationship ParentChildRelationship Form of the generated property A
      1 1 0 One-to-many relationship with no index
      1 1 0 One-to-many relationship with index on the many side
      1 This property if ParentChildRelationship is 1 1 Parent-child relationship
      0 0 0 List of objects
    • CascadeDeletes, which specifies whether to also generate an implementation of the %OnDelete() callback method in the generated classes. The generated %OnDelete() method deletes all persistent objects that are referenced by the class. Use this only if ParentChildRelationship is 0; a parent-child relationship already provides similar logic.

      Note:

      If you use this option and if you modify the generated classes, be sure to modify the %OnDelete() callback method as needed.

    • AddIdentity, which specifies how to generate InterSystems IRIS internal identifier for the objects (in the case of persistent classes). The choices are as follows:

      • "" — In this case, the system does not add any of the properties described here.

      • "ID" — In this case, the system adds the following property to each persistent class:

        Property %identity As %XML.Id (XMLNAME="_identity", XMLPROJECTION="ATTRIBUTE") [Transient];
      • "OID" — In this case, the system adds the following property to each persistent class:

        Property %identity As %XML.Oid (XMLNAME="_identity", XMLPROJECTION="ATTRIBUTE") [Transient];
      • "GUID" — In this case, the system adds the following property to each persistent class:

        Property %identity As %XML.GUID (XMLNAME="_identity", XMLPROJECTION="ATTRIBUTE") [Transient];

      See also Special Topics.

    For more information, see the class documentation for %XML.Utils.SchemaReaderOpens in a new tab.

  3. Optionally create an InterSystems IRIS multidimensional array to contain information about additional settings. For details see the Process() method in the class documentation for %XML.Utils.SchemaReaderOpens in a new tab.

  4. Invoke the Process() method of your instance:

    method Process(LocationURL As %String, 
                   Package As %String = "Test", 
                   ByRef Features As %String) as %Status {}
    • LocationURL must be the URL of the schema or the name of the schema file (including its complete path).

    • Package is the name of the package in which to place the generated classes. If you do not specify a package, InterSystems IRIS uses the service name as the package name.

    • Features is the multidimensional array that you optionally created in the previous step.

Default Data Types

For each property that it generates, %XML.Utils.SchemaReaderOpens in a new tab automatically uses an appropriate InterSystems IRIS data type class, depending on the XSD type specified in the schema. The following table lists the XSD types and the corresponding data types:

InterSystems IRIS Data Types Used for XML Types
XSD Type in Source Document Data Type in the Generated InterSystems IRIS Classes
anyURI %xsd.anyURIOpens in a new tab
base64Binary %xsd.base64BinaryOpens in a new tab or %Stream.GlobalBinaryOpens in a new tab, depending on options you chose. It is your responsibility to determine if each string might exceed the string length limit, and if so, to modify the generated property from %xsd.base64BinaryOpens in a new tab to an appropriate stream class.)
boolean %BooleanOpens in a new tab
byte %xsd.byteOpens in a new tab
date %DateOpens in a new tab
dateTime %TimeStampOpens in a new tab
decimal %NumericOpens in a new tab
double %xsd.doubleOpens in a new tab
float %xsd.floatOpens in a new tab
hexBinary %xsd.hexBinaryOpens in a new tab
int %xsd.intOpens in a new tab
integer %IntegerOpens in a new tab
long %IntegerOpens in a new tab
negativeInteger %xsd.negativeIntegerOpens in a new tab
nonNegativeInteger %xsd.nonNegativeIntegerOpens in a new tab
nonPositiveInteger %xsd.nonPositiveIntegerOpens in a new tab
positiveInteger %xsd.positiveIntegerOpens in a new tab
short %xsd.shortOpens in a new tab
string %StringOpens in a new tab (Note: It is your responsibility to determine if each string might exceed the string length limit, and if so, to modify the generated type to an appropriate stream class.)
time %TimeOpens in a new tab
unsignedByte %xsd.unsignedByteOpens in a new tab
unsignedInt %xsd.unsignedIntOpens in a new tab
unsignedLong %xsd.unsignedLongOpens in a new tab
unsignedShort %xsd.unsignedShortOpens in a new tab
no type given %StringOpens in a new tab

Property Keywords for the Generated Properties

For each property that it generates, %XML.Utils.SchemaReaderOpens in a new tab also automatically sets the following keywords, using information in the schema:

  • Description

  • Required

  • ReadOnly (if the corresponding element or attribute is defined with the fixed attribute)

  • InitialExpression (the value is taken from the fixed attribute in the schema)

  • Keywords related to relationships

Parameters for the Generated Properties

For each property that it generates, %XML.Utils.SchemaReaderOpens in a new tab automatically sets XMLNAME, XMLPROJECTION, and all other XML-related parameters as needed. For information on these, see Projecting Objects to XML. It also sets other parameters such as MAXVAL, MINVAL, and VALUELIST as appropriate.

Adjusting the Generated Classes for the Extremely Long Strings

In rare cases, you might need to edit the generated classes to accommodate extremely long strings or binary values, beyond the string length limit.

For any string types, an XML schema does not contain any information to indicate how long the strings might be. %XML.Utils.SchemaReaderOpens in a new tab maps any string values to the %StringOpens in a new tab class, and it maps any base64Binary values to the %xsd.base64BinaryOpens in a new tab class. These choices might not be appropriate, depending on the data that the class is intended to carry.

Before using the generated classes, you should do the following:

FeedbackOpens in a new tab