Book a Demo

Author Topic: Generate XML Schema for bi-directional associations  (Read 7432 times)

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Generate XML Schema for bi-directional associations
« on: January 09, 2016, 01:18:10 am »
I have a very simple example model. A bi-directional association between Class1 and Class2
Class1[1..1]<------->[1..*]Class2
Now I want to generate an XML Schema for this.
I would expect Class1 to reference Class2, and vice-versa, Class2 to reference Class1
But for some reason I only get Class1 that references Class2, but not the other way around.
This is what I get
Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Class1" type="Class1"/>
<xs:complexType name="Class1">
<xs:sequence>
<xs:element name="Class2" type="Class2" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Class2" type="Class2"/>
<xs:complexType name="Class2">
<xs:sequence/>
</xs:complexType>
</xs:schema>

And this is what I was expecting
Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Class1" type="Class1"/>
<xs:complexType name="Class1">
<xs:sequence>
<xs:element name="Class2" type="Class2" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Class2" type="Class2"/>
<xs:complexType name="Class2">
<xs:sequence>
<xs:element name="Class1" type="Class1" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Anyone know how to massage EA into getting it to produce the latter?

Thanks

Geert
« Last Edit: January 09, 2016, 01:19:52 am by Geert Bellekens »

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Generate XML Schema for bi-directional associations
« Reply #1 on: January 11, 2016, 11:00:58 am »
Hi Geert,

Can't help with the massage, but just to be clear; which class is the origin (source) and which is the destination (target).

Consequently, is the generated XML origin -> destination or vice versa?

My suspicion is a lack of functionality.  Bidirectional Associations are not processed correctly.  Part of the Navigability/Directionality conflation.

Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Generate XML Schema for bi-directional associations
« Reply #2 on: January 11, 2016, 02:19:01 pm »
Class1 is the source, and Class2 is the target.
But since those concepts (source/target) don't exist in UML I don't want EA take these into account.

I guess I'll have to send an email to support.

Geert

Rodrigo Nascimento

  • EA User
  • **
  • Posts: 33
  • Karma: +3/-0
    • View Profile
Re: Generate XML Schema for bi-directional associations
« Reply #3 on: January 12, 2016, 09:33:00 am »
Hi Geert,

I'm assuming you are using the "traditional" XSD generator...

As far as I remember, it uses the source/target to define the "path" it needs to follow for the schema hierarchy, where the destination is always going to be the child element. I had the same problem and tried many configurations related to Navigability (which I believe to be the correct one) and Directionality (as mentioned by Paolo) without success. My "workaround" at that point in time was creating another association in the opposite direction. If you import your expected XSD example into EA, you will see that this is how EA will interpret the XSD.

If you are not constrained by the EA version, I would suggest to use the Schema Composer as it includes all the associations "from and to" a specific class in the Schema Profile configuration.

Cheers,
Rod

 
Please check my blog for more EA related articles!

http://connection.rnascimento.com

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Generate XML Schema for bi-directional associations
« Reply #4 on: January 12, 2016, 02:22:21 pm »
Hi Rodrigo,

Yes, I'm using the traditional XML schema generator as I have to generate the schema for a whole data model. We also tried to use the Schema Composer for this, but that just isn't practical for that purpose.
I'll have a look at doubling up the association then.

Geert

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Generate XML Schema for bi-directional associations
« Reply #5 on: January 13, 2016, 08:11:25 pm »
Duplicating the associations seems to work.
Here's the relevant part of the script I use to prepare the XSD model to be generated as XML Schema
Code: [Select]
function fixConnectors(package)
dim SQLgetConnectors
dim connectors
set connectors = getConnectorsFromQuery(SQLgetConnectors)
dim connector as EA.Connector
'now get *all* associations between elements in this package in order to copy it in the other direction
SQLgetConnectors = "select c.Connector_ID from ((t_connector c " & _
" inner join t_object sob on c.Start_Object_ID = sob.Object_ID) " & _
" inner join t_object tob on c.End_Object_ID = tob.Object_ID) " & _
" where  " & _
" c.Connector_Type in ('Association','Aggregation') " & _
" and sob.Package_ID = "& package.PackageID & _
" and tob.Package_ID = "& package.PackageID
set connectors = getConnectorsFromQuery(SQLgetConnectors)
Session.Output "selectedconnectors: " & connectors.Count
for each connector in connectors
'copy the connector in the other direction if is an associations
dim target as EA.Element
dim copyConnector as EA.Connector
set target = Repository.GetElementByID(connector.SupplierID)
set copyConnector = target.Connectors.AddNew(connector.Name, connector.Type)
'set the supplierID
copyConnector.SupplierID = connector.ClientID
copyConnector.Direction = connector.Direction
'copy the connector ends
copyAssociationEnd connector.SupplierEnd, copyConnector.ClientEnd
copyAssociationEnd connector.ClientEnd, copyConnector.SupplierEnd
'save connector
copyConnector.Update
next
end function

function copyAssociationEnd(source, target)
' dim source as EA.ConnectorEnd
' dim target as EA.ConnectorEnd
target.Aggregation = source.Aggregation
target.Alias = source.Alias
target.AllowDuplicates = source.AllowDuplicates
target.Cardinality = source.Cardinality
target.Constraint = source.Constraint
target.Containment = source.Containment
target.Derived = source.Derived
target.DerivedUnion = source.DerivedUnion
target.IsChangeable = source.IsChangeable
target.Navigable = source.Navigable
target.Ordering = source.Ordering
target.OwnedByClassifier = source.OwnedByClassifier
target.Qualifier = source.Qualifier
target.Role = source.Role
target.RoleNote = source.RoleNote
target.StereotypeEx = source.StereotypeEx
target.Visibility = source.Visibility
'save changes
target.Update
end function