In an earlier post, I identified ISO20022 as a model-driven success story. Here, I give some other examples of model-driven from e-Government and telecommunications. They are not all at the same level of maturity, but the reason why a model-driven approach makes sense is the same.
How to Generate an XML Schema from MagicDraw – Part I
July 4th, 2012
One of the useful things you can do with MagicDraw is generate XML Schemas. We explain how to generate a valid XML schema from a simple MagicDraw class model.
What we are using
- MagicDraw 17.0.1 Enterprise Edition
- The Cameo Data Modeler
If you are using an earlier version of MagicDraw, you won’t need the Cameo Data Modeler.
You will need…
A class model in MagicDraw, your ‘source model’, ideally with a class diagram. For example, something like the following. The Payments package on the left contains classes and a diagram.
The main steps
Starting with the UML class model:
- Use ‘Model Transformations’ to transform the class model to a UML model of an XML Schema
- Make some small updates to the transformed model
- Use a ‘Code Engineering Set’ to generate an XML Schema (a .xsd file) from the transformed UML model
We will cover the first two steps in this post. Check out ‘How to Generate an XML Schema from MagicDraw – II‘ for details on the third step.
What the transformation step does
In step 1, elements from the ‘source’ model are created in a ‘target’ model and ‘stereotyped’ to identify how the elements in the class model are to be represented in XML schema. For example, the Payment class in the source model gets transformed into a Payment class stereotyped as <XSDcomplexType>, with properties stereotyped as <XSDelement>s, etc…
Doing a transformation
To transform the class model to an ‘XML Schema’ model, select the Menu ‘Tools->Model Transformations’.
This opens the Model Transformation Wizard. Select the UML to XML Schema transformation type. If you have not already included the modules needed for schema transformations, these will be automatically included by MagicDraw at this point.
Click Next, and select the ‘source’ for the transformation, that is, select the package containing the class model. In our example, we select the Payments package.
Select the destination for the transformation, the ‘target model’. We choose to place the transformation in a package called Payments Schema, and create a new package for this purpose.
At this point, click Finish, skipping steps 3, and 4 (accepting the defaults that MagicDraw uses), and the source model gets transformed.
The new Payments Schema package is populated with the transformation. Take a look at the tree below. You will see that the new Payments Schema package now contains a Payments package almost identical to the source package Payments. It contains classes of the same name as the classes in the source package, and a diagram of the same name as the original diagram.
Note that there is one addition – a Payments.xsd class. You will need to make some updates to this.
But first take a look at the diagram that was generated. You will see that the classes in the target model are stereotyped as XSD constructs.
You will also notice that the diagram is a bit of a mess. This is easy to correct. Select one of the options from the Layout menu, and you should get a diagram looking something like this:
Making updates to .xsd element in the transformed model
Look again at the newly created class, Payments.xsd in our example. This represents the XML schema itself. The name comes from the package name in the source model. Drag this class onto your class diagram.
You will see that it is stereotyped as an ‘XSDSchema’.
If this is to be a top level schema, you will need to add at least one ‘global element’ to the .xsd class (Payments.xsd in our example). We will create a global element of type ‘BulkPayments’, so that any XML file based on this XML schema will need to contain a global ‘BulkPayments’ element.
Create a property element on the class (right click New element->Property). Double click on the property to open the Specification dialog, type in a name (bulkPayment in our example), and choose the type. We set it to the BulkPayments class in the target package (from PaymentsSchema). Tip: If you start typing the name of the class in the entry field e.g. Bul, the list of classes starting with B will be displayed.
The property needs to be stereotyped as an ‘XSDElement’. Update the ‘Applied Stereotype’ field in the Specification dialog.
Your XSDschema class should now look something like this:
In case you want to set your own URL…
One of the tags on the class defines the namespace for the schema. The namespace is defaulted to the name of the package in the source – Payments in this case. If you want to change this to a URL, double click on the Payments.xsd class to open the Specification dialog, select tags and change the value of the Namespace tag e.g. to something of the form http ://my.namespace/ package. But this step isn’t essential.
You are now ready….
You are now ready to create an XML schema (.xsd file) using a code engineering set. Check out the blog post ‘How to Generate an XML Schema from MagicDraw – II’ for details.
One final important tip on rerunning transformations
If you make a change to classes in the source package, and transform again, the .xsd class will be created and the ‘global element’ property that you added manually will be removed. To avoid this, use the ‘update transformed model’ option. Select the target model in the tree (Payments Schema package in our example). Right click and select Tools>Update Transformed Model’ from the popup menu. Select the option to Change destination properties according to source, and ‘refresh’ the model.