This article provides a feedback about Sparx Enterprise Architect model transformation and XML Schema generation (XSD, JSON), involving the Schema Composer (introduced in Sparx EA v12).
Having a business class model in Enterprise Architect, I moved to the definition of the message structure for the application's services (purpose was to exchange data with another application involved in the overall process). I took the opportunity to assess EA model transformation and XML schema files generation based on the associated benefits:
- The existing Business Class diagram content can be reused to start the design.
- Traceability between the Business and Design models would enable impact analysis in the long run, whereas relevant information identified whilst carrying the design could be pushed to the business model.
- Enterprise Architect would facilitate publishing and sharing a model alongside XSD files with the dev team in charge of implementing the interfaced application. This step was required to agree on the exchanged data and associated services between both applications.
- Document generation features would facilitate producing Service Contract specifications.
Platform Independent Model (PIM)
Starting from a copy of the business model (CIM), modifications lead to the following PIM in the Design Model.
I ran Sparx Model transformation features to generate the XSD PSM model:
Resulting PSM model:
A number of issues were identified in the above PSM:
- XSDComplexType classes are generated with unstereotyped attributes, removing access to Enterprise Architect XSD built-in attribute stereotypes. Sparx EA "XML Schema" profile has useful attribute stereotypes, XSDElement and XSDAttribute, which are linked to the XML schema primitive data types. So it appears more suitable to generate XSD stereotyped attributes during the XSD model transformation.
- Attributes types are copied as is from the UML model e.g int, string, boolean... Defining XML data types in the PIM would prevent modifying the PSM after a model transformation run. This can be achieved through the use of attribute Tagged Values with a predefined list of the XML Schema data types. In turn custom model transformation rules would pick up this value when available to set the attribute type in the XSD PSM.
- Aggregation and composition links need to be replaced with directed associations in the PIM before running a model transformation. Custom XSD model transformation should carry these changes, as aggregration links in the PIM could be useful for other model transformations, such as implementation classes.
- Inheritance links seem to be supported, although Rodrigo Nascimento's blog suggests to create XSD Choices. EA Schema Composer latest updates appear to support inheritance links although I didn't manage to get it to work.
XSD enhancements in Sparx Enterprise Architect
Setting XSD data types in the PIM class attributes with a Tagged Value
A custom Tagged Value has been defined with the list of XSD primitive data types; when the XSDPrimitiveType tagged value is added to a class attribute, the suitable XSD primitive type can be selected. The custom Tagged Value type used in this article is available to download here.
Choosing between an XSD element or attribute in a PIM class attribute with a Tagged Value
By default class attributes will be transformed as XSDElement stereotyped attributes (see modification in the next section). The "xsdIsAttribute" tagged value has been created to provide an alternative so a PIM attribute is transformed to an XSDAttribute stereotyped attribute.
Custom model transformation templates for the Platform Specific Model (PSM)
Rules implemented in Enterprise Architect to generate an XSD PSM model have been modified as described in this section.
XSD attribute model transformation enhancements
1. If the xsdPrimitiveType tagged value is set, use it for the attribute data type, otherwise convert String and Date respectively to string and date, or copy the PIM attribute type as it is.
%if attTag:"xsdPrimitiveType"!=""%
type=%qt%%attTag:"xsd"%%qt%
%elseIf attType=="String"%
type="string"
%elseIf attType=="Date"%
type="date"
%else%
type=%qt%%attType%%qt%
%endIf%
2. If the xsdIsAttribute tagged value is set to "1", apply the XSDattribute stereotype, otherwise apply the XSDelement stereotype.
%if attTag:"xsdIsAttribute"=="1"% stereotype = "XSDattribute" %else% stereotype = "XSDelement" %endIf%
XSD connector model transformation enhancements
1. If the connector type is an aggregation, change it to a directed association.
$oldconnectorType = %connectorType%
%if connectorType=="Aggregation"%
Association
%else%
[...]
%if $oldconnectorType=="Aggregation"%
direction="Destination -> Source"
%endIf%
2. Applicable both in the association's Source and Target, set the aggregation value to None, and change an empty multiplicity to 1.
%if connectorSourceAggregation!="0"%
aggregation="0"
%endIf%
%if connectorSourceMultiplicity=="" and $oldconnectorType=="Aggregation" %
multiplicity="1"
%endIf%
The resulting PSM with the custom model transformation is illustrated below:
The custom XSD Transform Template used in this article can be downloaded from here. It can be installed using the menu Project | Data Management | Import Reference Data.
Hint: a link between a transformed class and the original one (PIM) can be seen in the element's Traceability view.
Schema Composer to generate XSD files
Enterprise Architect Schema Composer can be opened via the menu Tools | Schema Composer. Several profiles from the same PSM can be defined by selecting the message content, before generating the XSD or JSON file.
Create a new profile as illustrated below (select a package to store the profile as a Model Artifact).
Drag and drop the first class (Order), select all attributes and associations, and set it as Root (right click > set element as root). Once associations are added, the associated classes are automatically listed (their attributes and associations can be added to the XML Schema if needed).
Click on Update to save the following profile:
Click on Generate and select XSD to generate the XSD file. Generated XSD file content:
<?xml version="1.0" encoding="windows-1252"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:m="http://ns.umlchannel.com/services/order/v1.0" targetNamespace="http://ns.umlchannel.com/services/order/v1.0" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="order" type="order"/>
<xs:complexType name="order">
<xs:sequence>
<xs:element name="Order" type="Order" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="country" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="line1" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="line2" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="line3" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="postcode" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="town" minOccurs="1" maxOccurs="1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="LineItem">
<xs:sequence>
<xs:element name="quantity" minOccurs="1" maxOccurs="1" type=""/>
<xs:element name="unitprice" minOccurs="1" maxOccurs="1" type="xs:decimal"/>
<xs:element name="item" minOccurs="1" maxOccurs="1" type="StockItem"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Order">
<xs:sequence>
<xs:element name="date" minOccurs="1" maxOccurs="1" type="xs:date"/>
<xs:element name="ordernumber" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="ref" minOccurs="1" maxOccurs="1" type="xs:integer"/>
<xs:element name="lines" minOccurs="*" maxOccurs="unbounded" type="LineItem"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="StockItem">
<xs:sequence>
<xs:element name="cataloguenumber" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="costprice" minOccurs="1" maxOccurs="1" type="xs:integer"/>
<xs:element name="title" minOccurs="1" maxOccurs="1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Note:
- Elements generated in the XSD are sorted alphabetically ; it would be useful to change this order via the Schema Composer.
- Although the example here doesn't use any inheritance links, other tests showed that inheritance is not properly supported in the Schema Composer, despite the information from Sparx EA help guide.