I am currently trying to convert an XML Schema to ECore with the Eclipse Modeling Framework. EMF offers the attributre ecore:reference to indicate that an attribute should be translated into an EReference. However, this only works if the attribute is of type IDREF or anyURI. My problem is that any ID referenced via IDREF has to be unique througout the whole XML document. However, my XML has the following structure:
<A id="a1">
<B id="b1">
<ref idref="b2" />
</B>
<B id="b2">
<ref idref="b1" />
</B>
</A>
<A id="a2">
<B id="b1">
<ref idref="b2" />
</B>
<B id="b2">
<ref idref="b1" />
</B>
</A>
The ids of the B-elelemts are only unique inside the sourrounding A-element. Thus the parser will throw an error if i declare the ids of the B-elements as type ID.
How can I tell EMF that idref is a reference to the attribute id of B-element which belongs to the same A-element?
If the XML Schema is really using ID and IDREF, then the instance you show isn't valid with respect to that schema. Isn't that a problem? It's more like a key/keyref, which isn't supported by Ecore either.
If it just comes down to wanting an Ecore model that can read and write such instances I would define an attribute idref
that's just a string and define a transient reference resolvedRef
of type A and I'd modify the getters and setters so that each derives sensibly from the other. I.e., when you call getResolvedRef
, it would check if the field for that is null, and if the field for the idref
has a value, it would walk the model to resolve (look up that name in the appropriate scope) and store it in the field.
It's a bit tricky to define the mutual derivation in a sensible way for both getters and both setters, but it should be possible.