UML -> XSD questions

General discussions about code engineering

Moderator: Moderators

UML -> XSD questions

Postby P0rridge » Thu May 12, 2011 5:25 am

We have a UML class diagram. One class is modelled with sub-types, say A has sub types B and C. We want to end up with

<a>
<b...>
<c...>
</a>
where <a> is a choice.

but what we are getting is

<a> </a>
<b> </b>
<c> </c>

how do we make this work?
P0rridge
Forum Newbie
Forum Newbie
 
Posts: 11
Posts Rating:0
Joined: Tue Apr 20, 2010 3:10 am

Re: UML -> XSD questions

Postby TomasJkn » Tue May 17, 2011 10:18 am

Hello Gregory,

I am not 100pct sure what you are trying to achieve, but I'll try to answer here.
If you are trying to make XML schema, where there is top level element a, that has a choice of either b or c subelements, then schema like this would be needed:
ChoiceABC.png

(here complex types are named, but they can also be anonymous/inline)
This results in the following schema:
Code: Select all
<?xml version='1.0' encoding='windows-1257'?>

<xs:schema elementFormDefault="qualified" targetNamespace="http://my.name/space" xmlns="http://my.name/space" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:complexType name="A">
      <xs:choice>
         <xs:element name="b" type="B"/>
         <xs:element name="c" type="C"/>
      </xs:choice>
   </xs:complexType>
   <xs:complexType name="B"/>
   <xs:complexType name="C"/>
   <xs:element name="a" type="A"/>
</xs:schema>


If you want to make XML schema, where top level element a can have several subelements inside, each either b or c, then you would need a sequence-of-choices configuration. Like this:
ListOfChoicesABC.png

This results in the following schema:
Code: Select all
<?xml version='1.0' encoding='windows-1257'?>

<xs:schema elementFormDefault="qualified" targetNamespace="http://my.name/space" xmlns="http://my.name/space" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:complexType name="A">
      <xs:sequence>
         <xs:choice>
            <xs:element name="b" type="B"/>
            <xs:element name="c" type="C"/>
         </xs:choice>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="B"/>
   <xs:complexType name="C"/>
   <xs:element name="a" type="A"/>
</xs:schema>


Let me know if you had something else in mind. The problem description seems rather strange to me -
none of these layouts is semantically equivalent to UML inheritance case that you've described - class A, subclasses B & C ( well, the first variant is a little bit similar to this).
In any case if we are talking about possibility of transforming the UML model to such XML schema layouts, there is no functionality in transformation engine to achieve this kind of trick. Transformation can only transform UML generalizations to normal/usual extension generalizations in XML schema. So, anyway this case would have to be modeled by hand.

I hope this helps
You do not have the required permissions to view the files attached to this post.
Tomas Juknevicius
Senior Systems Analyst
No Magic Europe
TomasJkn
Customer Support
Customer Support
 
Posts: 63
Posts Rating:6
Joined: Mon May 10, 2010 9:24 am

Re: UML -> XSD questions

Postby P0rridge » Tue May 17, 2011 11:29 am

Thanks for the comprehensive reply.

2 questions arrise:

1) What is semantically equivelant to a sub-type super-type relationship?
2) If the UML->XSD transform wont do it in native form, what options are there for a script or macro to do this?

Thanks
P0rridge
Forum Newbie
Forum Newbie
 
Posts: 11
Posts Rating:0
Joined: Tue Apr 20, 2010 3:10 am

Re: UML -> XSD questions

Postby TomasJkn » Wed May 18, 2011 2:14 am

Hello again
1) What is semantically equivelant to a sub-type super-type relationship?

XML schemas have native subtype-supertype relationships. There are even two flavors thereof vs just one generalization flavor in UML. These two are - extension and restriction.
Restriction is probably not suitable in our case (mostly suitable for XSD simple type modeling, semantics roughly corresponds to UML DataType inheritance).
I think Extension is natural choice for modeling in this case. Actually that depends on the semantics of your classes A, B and C in UML.
If it is a usual kind, as Java/C++ coders understand - like, class A with several fields, class B inherited from A with several additional fields, class C inherited from A with several additional fields, then extension fits nicely.
Extension would not be too suitable in some narrow cases. E.g. if A is a union-type of B and C, your initial approach with choices may be more suitable.

So, if we go with extensions, then see the following schema model example (illustrating inheritance usage):
ExtensionsABC.png


this results in the following schema:
Code: Select all
<?xml version='1.0' encoding='windows-1257'?>

<xs:schema elementFormDefault="qualified" targetNamespace="http://my.name/space" xmlns="http://my.name/space" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:complexType name="A">
      <xs:sequence>
         <xs:element name="field1" type="xs:string"/>
         <xs:element maxOccurs="unbounded" minOccurs="0" name="children" type="A"/>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="B">
      <xs:complexContent>
         <xs:extension base="A">
            <xs:sequence>
               <xs:element name="field2" type="xs:string"/>
            </xs:sequence>
         </xs:extension>
      </xs:complexContent>
   </xs:complexType>
   <xs:complexType name="C">
      <xs:complexContent>
         <xs:extension base="A">
            <xs:sequence>
               <xs:element name="field3" type="xs:string"/>
            </xs:sequence>
         </xs:extension>
      </xs:complexContent>
   </xs:complexType>
   <xs:element name="a" type="A"/>
</xs:schema>


So we have 3 complex types, A, B, C. Elements of type A has subelement field1, of B - has subelements field1 field2, of C - has subelements field1, field3. Additionally any of these elements can have zero or more children elements. Children is declared to be of type A, but since B and C are extensions of A, elements of B, C types can be used as children. This illustrates one of the points of the generalization semantics - substitutability of elements.
Here is the example XML document, that is correct according to the schema above:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<a xmlns="http://my.name/space" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://my.name/space mychoices.xsd">
  <field1>sample text</field1>
  <children>
    <field1>sample text</field1>
    <children xsi:type="B">
      <field1>sample text</field1>
      <children xsi:type="B">
        <field1>sample text</field1>
        <children xsi:type="C">
          <field1>sample text</field1>
          <children xsi:type="B">
            <field1>sample text</field1>
            <field2>sample text</field2>
          </children>
          <children xsi:type="C">
            <field1>sample text</field1>
            <field3>sample text</field3>
          </children>
          <children>
            <field1>sample text</field1>
          </children>
          <field3>sample text</field3>
        </children>
        <children>
          <field1>sample text</field1>
          <children xsi:type="C">
            <field1>sample text</field1>
            <field3>sample text</field3>
          </children>
          <children xsi:type="B">
            <field1>sample text</field1>
            <field2>sample text</field2>
          </children>
          <children xsi:type="B">
            <field1>sample text</field1>
            <field2>sample text</field2>
          </children>
        </children>
        <field2>sample text</field2>
      </children>
      <field2>sample text</field2>
    </children>
  </children>
</a>


This document illustrates all the aspects of the schema above. Note how each child can be of any of the types A,B,C ; also children of type A have just field1, of type B - have field1, field2, of type C - field1, field3

2) If the UML->XSD transform wont do it in native form, what options are there for a script or macro to do this?

The standard UML->XSD transformation does generate extensions from UML generalizations. So it can be used for that.

If you want to perform some other additional operations on the model, there is always scripting capabilities in MagicDraw (accessible under Tools>Macros, supports popular scripting languages, like JavaScript, Python, Ruby), there is a QVT engine (brand new, added in v17.0, packaged in a separate plugin), and the old heavyweight solution - writing a dedicated plugin for MagicDraw in Java.
You do not have the required permissions to view the files attached to this post.
Tomas Juknevicius
Senior Systems Analyst
No Magic Europe
TomasJkn
Customer Support
Customer Support
 
Posts: 63
Posts Rating:6
Joined: Mon May 10, 2010 9:24 am

Re: UML -> XSD questions

Postby TomasJkn » Wed May 18, 2011 2:20 am

I forgot to mention one more thing. There is a concept called substitution groups in XML schema. It allows to specify that element b can appear in place of element a.
May be this is closer to your intended layout of xml document?
If you would be interested in these, drop me a note, I'll try to explain those in more detail. But these things are modeled differently in MagicDraw - not related to generalizations.
Tomas Juknevicius
Senior Systems Analyst
No Magic Europe
TomasJkn
Customer Support
Customer Support
 
Posts: 63
Posts Rating:6
Joined: Mon May 10, 2010 9:24 am


Return to Code engineering

Who is online

Users browsing this forum: No registered users and 0 guests