XML Schema Generation

The XSD Generation facility converts a UML class model to a W3C XML Schema (XSD). This allows Data Modelers to start working at a conceptual level in UML, leaving the tedious aspects of XSD creation to EA. The schema generation can then be customized if necessary, by using the provided "UML Profile for XML" as described later.

An XML schema corresponds to a UML package. Therefore the XSD generation is a package-level operation in EA. To demonstrate the usage of the schema generator, we begin with an example model.

Getting Started
To use the schema generation facility you will require the following:

EA Professional or Corporate edition
XSDDataTypes Package: This package contains classes representing XSD primitive data types.
UML Profile for XML: This resource file contains the stereotyped classes which allow the schema generation to be customized.

Note: the XSDDataTypes package is available from the above link as an XMI file. Once you have downloaded the file, use EA's "Import from XML file" facility to import the package (see more information in the EA helpfile under Model Management | Import & Export | XMI | Import from XMI).

Steps to Generate XSD:

1. Select the package to be converted to XSD by right-clicking on the package in the Project Browser.
2. Select Project | Generate XML Schema from the main menu.
3. Set the desired output file using the Filename field.
4. Set the desired xml encoding using the Encoding field.
5. Click on the Generate button to generate the schema.
6. The progress of the schema generator will be shown in the Progress edit box.

Example
The following Class diagram models a simple "Employee Details" system, intended to store a company's employee contact information. The classes shown form the "EmployeeDetails" package. The UML attributes of the classes map directly to XML elements or attributes. Note that the classes have no methods, since there is no meaningful correspondence between class methods and XSD constructs.

The following figure shows the schema which is generated for the Employee Details package by default. Notice how each UML class corresponds to a complexType definition in the schema. The class attributes are generated as schema elements contained in a "sequence" model group within the definition. The enumeration class is the exception here- it maps directly to an XSD enumeration, contained within a simpleType definition.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="Person" type="Person"/>
   <xs:complexType name="Person">
      <xs:sequence>
         <xs:element name="Person.firstName" type="xs:string"/>
         <xs:element name="Person.surName" type="xs:string"/>
         <xs:element name="Person.birthDate" type="xs:string"/>
         <xs:element name="Person.gender" type="xs:string"/>
         <xs:element name="Person.contactDetails">
            <xs:complexType>
               <xs:sequence>
                  <xs:element ref="ContactInfo"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
      </xs:sequence>
   </xs:complexType>
   <xs:element name="Employee" type="Employee"/>
   <xs:complexType name="Employee">
      <xs:complexContent>
         <xs:extension base="Person">
            <xs:sequence>
               <xs:element name="Employee.status" type="Status"/>
               <xs:element name="Employee.jobTitle" type="xs:string"/>
               <xs:element name="Employee.startDate" type="xs:date"/>
               <xs:element name="Employee.department" type="xs:string"/>
            </xs:sequence>
         </xs:extension>
      </xs:complexContent>
   </xs:complexType>
   <xs:element name="ContactInfo" type="ContactInfo"/>
   <xs:complexType name="ContactInfo">
      <xs:sequence>
         <xs:element name="ContactInfo.homePhone" type="xs:string"/>
         <xs:element name="ContactInfo.mobilePhone" type="xs:string"/>
         <xs:element name="ContactInfo.officePhone" type="xs:string"/>
         <xs:element name="ContactInfo.email" type="xs:string"/>
         <xs:element name="ContactInfo.streetAddress" type="xs:string"/>
      </xs:sequence>
   </xs:complexType>
   <xs:simpleType name="Status">
      <xs:restriction base="xs:string">
         <xs:enumeration value="Full-Time"/>
         <xs:enumeration value="Part-Time"/>
         <xs:enumeration value="Casual"/>
         <xs:enumeration value="Contract"/>
      </xs:restriction>
   </xs:simpleType>
   <xs:element name="EmployeeRecords" type="EmployeeRecords"/>
   <xs:complexType name="EmployeeRecords">
      <xs:sequence>
         <xs:element name="EmployeeRecords.Employee">
            <xs:complexType>
               <xs:sequence>
                  <xs:element ref="Employee" minOccurs="0" maxOccurs="unbounded"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
      </xs:sequence>
   </xs:complexType>
</xs:schema>

The following table describes the default mapping of UML constructs to XSD constructs. The next section describes a UML Profile for XML which allows this default mapping to be altered to suit individual needs. Using the profile, we will alter our example class model to tailor the generation of the XML schema.

UML Construct Default XSD Production Rules
Package

A schema element is generated for the target package. If the target package includes classes from another package, which has the tagged values “targetNamespace” and “targetNamespacePrefix” set, these are included as attributes of the schema element.

In addition, an import or include element is created for each referenced package. (An include element is used if the external package shares the same targetNamespace tagged value as the target package. An import element is used where the targetNamespaces differ.)

Class A root-level element declaration and complexType definition are generated. The element name and type are the same as the class name. An XSD sequence model group is generated to contain UML attributes generated as elements.
Attribute An element is declared for each class attribute. The element name is set to that of the UML attribute name. This is prepended with the class name to make the element unique. The minOccurs and maxOccurs attributes are set to reflect the attribute cardinality. (Note: If left unspecified, minOccurs and maxOccurs default to 1.) If the attribute refers to another class, the element declaration is followed a complexType definition, which contains a reference to the appropriate complexType.
Association An element is declared for each of association owned by a class. The element name is set to that of the association role. The minOccurs and maxOccurs reflect the cardinality of the association. Note: if the direction of the association is unspecified, the owner is assumed to be the source.
Generalization (Inheritance) For single inheritances, an extension element is generated with the base attribute set to the base class name. The UML attributes of the child class are then appended to an all model group within the the extension element.
<<enumeration>> (stereotype) A simpleType element is declared for the enumeration class with the name attribute set to the class name. A restriction element is generated with base set to string. Each of the class attributes are appended to the restriction element as XSD enumeration elements with value set to the UML attribute name. Any type specification for the UML attributes will be ignored by the schema generator.

UML Profile for XML
The UML profile for XML specifies a set of stereotypes, tagged values and constraints which may be applied to the UML model in order to change particular aspects of the resulting schema. For example, we may wish to have certain UML class attributes converted to XSD attributes or, we may need to use a different model group than the default "sequence".

The stereotype explicitly tells the generator to which XSD structure the UML construct maps. The tagged values further define aspects of the mapping, such as whether the elements should be qualified. The constraints define any conditions that must be satisfied for the stereotype to apply.

To demonstrate how the profile for XML can affect the schema generation, refer to the following class diagram - an adaptation of our previous example. Note the changes in the corresponding schema.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:simpleType name="Gender">
      <xs:restriction base="xs:string">
         <xs:pattern value="male|female"/>
      </xs:restriction>
   </xs:simpleType>
   <xs:element name="Person" type="Person"/>
   <xs:complexType name="Person">
      <xs:sequence>
         <xs:element name="Person.surName" type="xs:string"/>
         <xs:element name="Person.firstName" type="xs:string"/>
         <xs:element name="Person.birthDate" type="xs:string"/>
         <xs:element name="Person.contactDetails">
            <xs:complexType>
               <xs:sequence>
                  <xs:element ref="ContactInfo"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
      </xs:sequence>
      <xs:attributeGroup ref="Person.att"/>
   </xs:complexType>
   <xs:attributeGroup name="Person.att">
      <xs:attribute name="Person.gender" type="Gender"/>
   </xs:attributeGroup>
   <xs:element name="Employee" type="Employee"/>
   <xs:complexType name="Employee">
      <xs:complexContent>
         <xs:extension base="Person">
            <xs:sequence>
               <xs:element name="Employee.status" type="Status"/>
               <xs:element name="Employee.jobTitle" type="xs:string"/>
               <xs:element name="Employee.startDate" type="xs:date"/>
               <xs:element name="Employee.department" type="xs:string"/>
            </xs:sequence>
         </xs:extension>
      </xs:complexContent>
   </xs:complexType>
   <xs:element name="ContactInfo" type="ContactInfo"/>
   <xs:complexType name="ContactInfo">
      <xs:sequence>
         <xs:element name="homePhone" type="xs:string"/>
         <xs:element name="email" type="xs:string" maxOccurs="3"/>
         <xs:element name="streetAddress" type="xs:string"/>
         <xs:choice>
            <xs:element name="mobilePhone" type="xs:string"/>
            <xs:element name="officePhone" type="xs:string"/>
         </xs:choice>
      </xs:sequence>
   </xs:complexType>
   <xs:simpleType name="Status">
      <xs:restriction base="xs:string">
         <xs:enumeration value="Full-Time"/>
         <xs:enumeration value="Part-Time"/>
         <xs:enumeration value="Casual"/>
         <xs:enumeration value="Contract"/>
      </xs:restriction>
   </xs:simpleType>
   <xs:element name="EmployeeRecords" type="EmployeeRecords"/>
   <xs:complexType name="EmployeeRecords">
      <xs:all>
         <xs:element name="EmployeeRecords.Employee">
            <xs:complexType>
               <xs:sequence>
                  <xs:element ref="Employee" minOccurs="0" maxOccurs="unbounded"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
      </xs:all>
   </xs:complexType>
</xs:schema>

In particular:

We have explicitly set the stereotype for the Person and ContactInfo classes to XSDcomplexType. EmployeeRecords uses the modelGroup tagged value to use the all construct instead of the default sequence. The ContactInfo also uses the memberNames tag to prevent the attribute's element names being prefixed with the class name.
The XSDattribute stereotype causes the generation of an XSD attribute (instead of the default element), for the gender attribute of class Person.
To restrict the possible values for the gender attribute to "male" or "female", we have created the Gender class. Class "Gender" is stereotyped XSDsimpleType and uses the pattern tagged value to restrict its possible values. We could also have used regular expressions in this way to restrict values for the "birthDate", "email" and phone number attributes.
The use of the XSDchoice stereotype allows us to generate the "mobilePhone" and "officePhone" attributes as options within an XSD choice modelGroup. Note how the memberNames of attributes contained by this modelGroup are unqualified as determined by its containing complexType class. (This occurs for each owner of the modelGroup, where the multiple associated classes.)
Less obvious from the diagram is the ordering of the attributes for the Person class. Notice how the "firstName" and "surName" attributes have had their position swapped in the schema. This is because the position tagged value has been used for the attribute of the Person class in the modified example.

The following table details the features of the UML Profile for XML. Tagged value names are shown in bold followed by the allowed values. If there is a default value used by EA's schema generator it is underlined.

<<XSDschema>>

UML Construct Package; Component
Description All classes in a package or component are defined within one schema. This stereotype can be used to specify schema-wide settings.
Tagged Values
anonymousRole:

( true | false )
Specifies if the role name is included in the element declaration for the UML attribute
anonymousType:

( true | false )

Specifies whether the class type is anonymous for attributes.
attributeFormDefault:

( qualified | unqualified )
Determines whether attribute instances must be qualified
defaultNamespace:


The default namespace used in this schema. This value is used to specify the default namespace attribute (xmlns=), in the schema element.
elementDerivation:

( true | false )
Determines whether inheritances are generated using XSD extension or copy-down inheritance.
elementFormDefault:

( qualified | unqualified )
Determines whether element instances must be qualified.
memberNames:

( qualified | unqualified )
Determines whether elements generated from Class attributes have their name qualified by the corresponding class name.
modelGroup:

( all | sequence | choice )
Specifies the default XSD model used to generate complexType definitions.
schemaLocation: The URI which identifies the location of the schema. This value is used in the import and include elements.
targetNamespace: The URI which uniquely identifies this schema's namespace.
targetNamespacePrefix: The prefix which abbreviates the targetNamespace.
version: The version of this schema.
Constraints None.

<<XSDcomplexType>>

UML Construct Class
Description complexType definitions are created for generic UML classes. This stereotypes helps tailor the generation of a complexType definition
Tagged Values
memberNames:

( qualified| unqualified )
Determines whether elements generated from the UML class attributes and associations have their name qualified by the corresponding class name for this complexType definition.
mixed:

( true | false )
Determines whether this element may contain mixed element and character content. Refer to the W3C XML Schema recommendation.
modelGroup:

( all | sequence | choice )
Overrides the default XSD model for generating this complexType definition.
Constraints None.

<<XSDsimpleType>>

UML Construct Class
Description An XSD simpleType is generated for classes with this stereotype.
Tagged Values
derivation:

( restriction | list )
Specifies the derivation of the simpleType. Refer to the W3C XML Schema recommendation.
length: Refer to the W3C XML Schema recommendation.
minLength: Refer to the W3C XML Schema recommendation.
maxLength: Refer to the W3C XML Schema recommendation.
pattern: Refer to the W3C XML Schema recommendation.
Constraints This class can only participate in an inheritance relation with another simpleType. It cannot have any attributes or own any associations. They will be ignored if present.

<<XSDsequence>>

UML Construct Class
Description The schema generator creates a sequence model group as the container for the attributes and associations owned by this class. The model group is in turn added to the model groups of this class' respective owners.

Note: Tagged values specified by owners of this class persist through to the child elements of this model group. Thus if memberNames are unqualified for a complexType, so will be the children of this model group when added to that complexType.
Tagged Values None.
Constraints This class must be the destination of unidirectional associations. If it is not, this class and its connectors are ignored, possibly invalidating other model group classes. Inheritance relations are ignored for this class.

<<XSDchoice>>  

UML Construct Class
Description Creates an XSD choice element. Refer to XSDsequence for more details.
Tagged Values None
Constraints As for XSDsequence.

<<XSDelement>>

UML Construct Attribute; AssociationEnd
Description By applying this stereotype to a UML class attribute or AssociationEnd, the corresponding UML entity is generated as an element within the parent complexType and not as an XSD attribute.
Tagged Values
form:

( qualified | unqualified )
Overrides the schema's elementFormDefault value.
position: Causes the the elements to be ordered within a sequence model group of the containing complexType. Duplicated and invalid position tagged values are ignored and result in undefined ordering of the UML attributes. Missing position values cause the defined positions to be allocated as specified, with the remaining elements filling the missing positions in an undefined order.
anonymousRole:

( true | false )
Specifies if the role name is included in the element declaration for the UML attribute
anonymousType:

( true | false )

Specifies whether the class type is anonymous for attributes.
Constraints None.

<<XSDattribute>>

UML Construct Attribute; AssociationEnd
Description By applying this stereotype to a UML class attribute or AssociationEnd, the corresponding UML entity is generated as an XSD attribute within the parent complexType and not as an XSD element.
Tagged Values
form:

( qualified | unqualified )
Overrides the schema's attributeFormDefault value.
use:

(prohibited | optional | required )
Refer to the W3C XML Schema recommendation.
default: Refer to the W3C XML Schema recommendation.
fixed: Refer to the W3C XML Schema recommendation.
Constraints The attribute datatype should not refer to a class specification, it will be ignored otherwise.

<<XSDany>>

UML Construct Class; Attribute
Description If applied to a UML attribute, an XSD anyAttribute element is generated. If applied to a UML class, an XSD any element is generated.
Tagged Values  
namespace: Refer to the W3C XML Schema recommendation.
processContents:

( skip | lax | strict )
Refer to the W3C XML Schema recommendation.
Constraints None.

<<XSDrestriction>>

UML Construct Generalization
Description Overrides the default use of XSD extension for inheritance and generates the child as a complexType with a restriction element instead.
Tagged Values None
Constraints Applies only to UML class parent-child relations.

To Do:

More XSD stereotypes will be added to the profile such as XSDattributeGroup, XSDgroup and XLink support.
More tagged values will be added, such as allowing suffixes for complexType and simpleType definitions.
More detailed logging of errors and warnings during schema generation.
Generalization and Association are the only currently supported connection types. Aggregation (and Composition) will be added.

Known Issues:

Only limited inter-package support is currently available. For example, a class which inherits from multiple classes outside of the target package does not get all base attributes.

Please note that this functionality is available in EA Professional and EA Corporate.