0

it's going to be a long post, thank you for readying :-) This question is from a one level up "view" from this post:

Storing meta information to class properties

We have a XSD from which we are going to generate classes from (C#/Java). So the problem is split into two different parts: Define it in the XSD, then create the datamodel from it.

We want to have some elements in a complex type to carry meta information, but just in certain x-paths in the XSD. That means that when a type is reused as a element in another complex type we want to tell the user whether it is "relevant" for something (I called it security relevant in my other post). The relevance is denoted in several levels. (That explanation also applies for the datamodel in C#/Java ofcourse)

XSD Type Example:

<xs:complexType name="Core">
  <xs:sequence>
    <xs:element name="No" type="xs:int" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="ComplexA">
  <xs:sequence>
    <xs:element name="Info" type="Core" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="ComplexB">
  <xs:sequence>
    <xs:element name="Info" type="Core" />
  </xs:sequence>
</xs:complexType>

The Core type has a property "No". This property can be relevant in other types. For example, it is supposed to be "relevant-level-1" in type ComplexA.Info, in ComplexB.Info it's not.

We came up with two different ways to achieve this:

1: Introduce a attribute on type level:

<xs:complexType name="ComplexA">
  <xs:sequence>
    <xs:element name="Info" type="Core" />
  </xs:sequence>
  <xs:attribute name="Relevance.Info.No" type="RelevanceType" fixed="Relevance1"/>
</xs:complexType>

<xs:simpleType name="RelevanceType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="Relevance1" />
    <!-- Some more relevance values -->
  </xs:restriction>
</xs:simpleType>

The attribute "Relevance.Info.No" basically tells that the property "No" in the member "Info" has the relevance type "Relevance1". This approach has obviously some drawbacks. The class generation is very complicated since we would have to manually process this attribute information. A solution for that is posted in the thread mentioned in the beginning.

2: Introduce another complex type for every property with a relevance level:

This will solve the way this "meta data" is attached to the properties, but will apperently produces a lot classes on XSD and C#/Java for every combination of relevance level within a type.

As you can imagine the problems for the creation of the datamodel on C# and Java side are very similar to the problems within the XSD. The best way would be to have XSD ready to automatically create the java classes (JaxB) and C# classes (XGen) without any other manipulation of the generated code (which I'm doing right now).

Do you see any other possibilities to achieve that?

Any help is appreciated!

Community
  • 1
  • 1
DerApe
  • 3,097
  • 2
  • 35
  • 55
  • Is the relevance of properties in given contexts a function of the instance, or is it purely a function of the class system? From the use of a `fixed` value in your example, I guess it won't vary in the instances; if that's so, I wonder if an extension attribute on the `element` reference would suffice? ` – C. M. Sperberg-McQueen Sep 17 '12 at 19:42
  • @C.M.Sperberg-McQueen: The relevance CAN vary from modelpath to modelpath, its just fixed for all intances of that particular type hierarchy. Concerning the attribute extension: Either way this will only simplify the syntax, correct? – DerApe Sep 18 '12 at 06:32

2 Answers2

0

In this case I believe you need to use inheritance. If you define one more type called CoreBase and if you Inherit this type in Core

You can use Core in ComplexA and you can use CoreBase in ComplexB

<xs:complexType name="CoreBase">
  <xs:sequence>
    ... some attributes or how ever you wish
  </xs:sequence>
</xs:complexType>

<complexType name="Core">
    <complexContent>
        <extension base="CoreBase">
            <sequence>
                <xs:element name="No" type="xs:int" />
            </sequence>
        </extension>
    </complexContent>
</complexType>

<xs:complexType name="ComplexA">
  <xs:sequence>
    <xs:element name="Info" type="Core" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="ComplexB">
    <xs:sequence>
        <xs:element name="Info" type="CoreBase" />
    </xs:sequence>
</xs:complexType>

Please see the link for inheritance in xml schema Xsd and inheritance

Community
  • 1
  • 1
Ozgur
  • 258
  • 3
  • 13
  • Thanks for the reply, but as I stated, this will create a lot of "base classes", because there can be several combinations of attributes. Right now I don't think that there is a clean solution for this problem using the XSD technology. It is just not ment to be used like this. – DerApe Oct 22 '12 at 06:30
0

Well as I stated in my comment to the answer, I don't think there is a clean solution to this. Maybe XML is not intended to be used like this.
But I would be still interested in other solutions...

DerApe
  • 3,097
  • 2
  • 35
  • 55