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. This package is available as an XMI file. To import the file as a UML Package, use EA's XMI import facility which is available from the menu item: Project | Import/Export | Import Package from XMI.
- UML Profile for XML: This resource file contains the stereotyped classes which allow the schema generation to be customized. The UML Profile for XML can be imported into a model using the Resource View (see Importing Profiles for details on importing UML profiles into EA).
Steps to Generate XSD:
- Select the package to be converted to XSD by right-clicking on the package in the Project Browser.
- Select Project | Generate XML Schema from the main menu.
- Set the desired output file using the Filename field.
- Set the desired xml encoding using the Encoding field.
- Click on the Generate button to generate the schema.
- 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"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <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="Person" type="Person"/> <xs:complexType name="Person"> <xs:sequence> <xs:element name="firstName" type="xs:string"/> <xs:element name="surName" type="xs:string"/> <xs:element name="birthDate" type="xs:string"/> <xs:element name="gender" type="xs:string"/> <xs:element name="contactDetails" type="ContactInfo"/> </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="status" type="Status"/> <xs:element name="jobTitle" type="xs:string"/> <xs:element name="startDate" type="xs:date"/> <xs:element name="department" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:element name="EmployeeRecords" type="EmployeeRecords"/> <xs:complexType name="EmployeeRecords"> <xs:sequence> <xs:element name="Employee" type="Employee" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:element name="ContactInfo" type="ContactInfo"/> <xs:complexType name="ContactInfo"> <xs:sequence> <xs:element name="homePhone" type="xs:string"/> <xs:element name="mobilePhone" type="xs:string"/> <xs:element name="officePhone" type="xs:string"/> <xs:element name="email" type="xs:string"/> <xs:element name="streetAddress" type="xs:string"/> </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 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"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="ContactInfo" type="ContactInfo"/> <xs:complexType name="ContactInfo"> <xs:all> <xs:element name="ContactInfo.homePhone" type="xs:string" maxOccurs="1"/> <xs:element name="ContactInfo.email" type="xs:string" maxOccurs="3"/> <xs:element name="ContactInfo.streetAddress" type="xs:string"/> <xs:choice> <xs:element name="ContactInfo.mobilePhone" type="xs:string"/> <xs:element name="ContactInfo.officePhone" type="xs:string"/> </xs:choice> </xs:all> </xs:complexType> <xs:simpleType name="Gender"> <xs:restriction base="xs:string"> <xs:pattern value="male|female"/> </xs:restriction> </xs:simpleType> <xs:element name="Employee" type="Employee"/> <xs:complexType name="Employee"> <xs:complexContent> <xs:extension base="Person"> <xs:sequence> <xs:element name="status" type="Status"/> <xs:element name="jobTitle" type="xs:string"/> <xs:element name="startDate" type="xs:date"/> <xs:element name="department" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:element name="Person" type="Person"/> <xs:complexType name="Person"> <xs:sequence> <xs:element name="surName" type="xs:string" maxOccurs="1"/> <xs:element name="firstName" type="xs:string" maxOccurs="1"/> <xs:element name="birthDate" type="xs:string" maxOccurs="1"/> <xs:element name="contactDetails" type="ContactInfo"/> </xs:sequence> <xs:attribute name="gender" use="optional" type="Gender"/> </xs:complexType> <xs:element name="EmployeeRecords" type="EmployeeRecords"/> <xs:complexType name="EmployeeRecords"> <xs:all> <xs:element name="Employee" type="Employee" minOccurs="0" maxOccurs="unbounded"/> </xs:all> </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: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 class also uses the memberNames tag to allow the attribute's element names to be 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 qualified 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 | |
Description | All classes in a package 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 group used to generate complexType definitions. | |
schemaLocation: | The URI that identifies the location of the schema. This value is used in the import and includes elements. | |
targetNamespace: | The URI that uniquely identifies this schema's namespace. | |
targetNamespacePrefix: | The prefix that abbreviates the targetNamespace. | |
version: | The version of this schema. | |
Constraints | None. |
<<XSDgroup>>
UML Construct | Class | |
Description | An XSDgroup is generated for classes with this stereotype. | |
Tagged Values |
modelGroup: (sequence | choice | all) |
Overrides the default XSD model for generating this group definition. |
Constraints |
A group class can only associate itself to other group classes. A group class can be associated by another group class or a complexType class. The association should be via an association link. A group class cannot be inherited/aggregated. |
<<XSDtopLevelElement>>
UML Construct | Class | |
Description | Creates an <xs:element> construct which acts as a container for XSDcomplexType and XSDsimpleType class. | |
Tagged Values | None. | |
Constraints | An XSDtopLevelElement class can contain either an XSDsimpleType or an XSDcomplexType as its child class. When such a class is present as its child, all its inheritance is ignored. This class cannot be inherited. |
<<XSDtopLevelAttribute>>
UML Construct | Class | |
Description | Creates an <xs:attributr> construct which acts as a container for XSDsimpleType class | |
Tagged Values |
use: (optional | required | prohibited) |
See the W3C XML Schema recommendation. |
Constraints | An XSDtopLevelAttribute class can contain only an XSDsimpleType class as its child class. When such a class is present as its child, all its inheritance is ignored. This class can inherit from only one XSDsimpleType class. |
<<XSDunion>>
UML Construct | Class | |
Description | Creates an <xs:union> construct which can act as a container for XSDsimpleType class. | |
Tagged Values | None. | |
Constraints | An XSDunion class can contain only XSDsimpleType as its child class and can generalize from other XSDsimpleType classes only. All the classes that this class generalizes become the members of the attribute memberTypes. This class cannot have any attributes or associations. |
<<XSDattributeGroup>>
UML Construct | Class | |
Description | Creates an <xsattributeGroup> construct which can act as a container for a set of elements for stereotype XSDattribute. | |
Tagged Values | None. | |
Constraints | An XSDattributeGroup class can contain only elements of stereotype XSDattribute and can be associated only with other XSDattributeGroup classes. Only XSDcomplexType classes can associate with this class. This class cannot be inherited. |
<<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: | See the W3C XML Schema recommendation. | |
minLength: | See the W3C XML Schema recommendation. | |
maxLength: | See the W3C XML Schema recommendation. | |
minInclusive: | See the W3C XML Schema recommendation. | |
minExclusive: | See the W3C XML Schema recommendation. | |
maxInclusive: | See the W3C XML Schema recommendation. | |
maxExclusive: | See the W3C XML Schema recommendation. | |
totalDigits: | See the W3C XML Schema recommendation. | |
fractionDigits: | See the W3C XML Schema recommendation. | |
whiteSpace: | See the W3C XML Schema recommendation. | |
pattern: | See 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 are 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 are 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 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) |
See the W3C XML Schema recommendation. | |
default: | See the W3C XML Schema recommendation. | |
fixed: | See 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: | See the W3C XML Schema recommendation. |
processContents: (skip | lax | strict) |
See 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. |
Please note that this functionality is available in EA Professional and EA Corporate.