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 II
July 6th, 2012
In our first post on generating an XML Schema from MagicDraw, we showed how to transform a class model into a model of an XML Schema. The final step in the process is to generate an actual XML Schema (a .xsd file) from the transformed model using a ‘Code Engineering Set’.
You will need…
To have followed the steps in the first post!
Create a code engineering set
You may or may not see a ‘Code engineering sets’ node in the MagicDraw containment tree. If you don’t, click on the ‘Show Code Engineering Sets’ icon at the top of the tree to make the node visible.
To create a code engineering set, select the ‘Code engineering sets’ node in the tree, right click and select New->XML Schema from the popup menu.
You will be prompted to give the set a name. In our example, we use PaymentsSchema.
A new node then appears in the tree.
Configure (edit) the code engineering set
You will need to identify the model that is the basis for the schema generation (the transformed model). Click on the new node, and select ‘Edit…’ from the popup menu.
In the code engineering set dialog, select the tab ‘Add Data from Model’. Select the package containing your transformed model (‘Payments Schema’ in our example), and click ‘Add Recursively’. An entry ‘schema.xsd’ will appear in the right hand column.
Close the dialog.
Rename the ‘schema.xsd’
By default, the name of the generated schema is ‘schema.xsd’. But it is useful to give your XML schema file a meaningful name. The name of the .xsd node under code engineering sets in the tree is what determines the name of the XML schema file. We recommend you rename it. Select schema.xsd and from the popup menu, select Rename. In our case, we rename to paymentsSchema.xsd.
You will notice that a File View package has appeared in the tree. This contains a component element representing the schema file. You don’t need to do anything with this!
Generate the XML schema
You are now ready to generate the schema. Select the .xsd node, right click and select ‘Generate’ from the popup.
You will need to select the output directory. We use the default in our example – the install location for MagicDraw.
Your schema should now be generated.
Locate the .xsd file in the output directory and open in an XML editor. Check what it looks like and make sure it is a valid schema – most XML editors will provide an option to check the schema is valid. Here’s a visual representation of what the schema looks like for our payments example:
And try generating an XML file from it – again, most XML editors will give you this option. Here’s what an XML file based on our example schema.
Now you can add OCL rules…
Once the schema generation is working, you can start adding OCL business rules to the MagicDraw model, and generate executable rulesets from the OCL. Use the XML schema to validate that your XML files are syntactically correct, and use the executable rulesets to validate that the content of the XML files meets your business rules. Take a look at the Cameo OCL Transfomer product for details.
And don’t forget the tip about rerunning transformations
When you make changes to the classes in the source package, you will need to run a transformation again – and regenerate the schema. If you run the transformation from scratch, the changes you made to add a global element to the .xsd class in the transformed model will be lost. Instead, you can update the transformed model. 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.
To regenerate the XML schema, just select .xsd node under Code engineering sets again, and select the ‘Generate’ option from the popup menu. The XML schema file is regenerated and will reflect the changes made to your source model (new classes, changed properties etc).