Meshing different systems of keys together in XML Schema

Posted by Tom W on Stack Overflow See other posts from Stack Overflow or by Tom W
Published on 2010-04-24T15:46:30Z Indexed on 2010/04/24 15:53 UTC
Read the original article Hit count: 193

Filed under:

Hello SO,

I'd like to ask people's thoughts on an XSD problem I've been pondering. The system I am trying to model is thus:

I have a general type that represents some item in a hypothetical model. The type is abstract and will be inherited by all manner of different model objects, so the model is heterogeneous. Furthermore, some types exist only as children of other types.

Objects are to be given an identifier, but the scope of uniqueness of this identifier varies. Some objects - we will call them P (for Parent) objects - must have a globally unique identifier. This is straightforward and can use the xs:key schema element. Other objects (we can call them C objects, for Child) are children of a P object and must have an identifier that is unique only in the scope of that parent. For example, object P1 has two children, object C1 and C2, and object P2 has one child, object C3. In this system, the identifiers given could be as follows:

P1: 1 (1st P object globally)
P2: 2 (2nd P object globally)
C1: 1 (1st C object of P1)
C2: 2 (2nd C object of P1)
C3: 1 (1st C object of P2)

I want the identity syntax of every model object to be identical if possible, therefore my first pass at implementing is to define a single type:

  <xs:complexType name="ModelElement">
    <xs:attribute name="IDMode" type="IdentityMode"/>
    <xs:attribute name="Identifier" type="xs:string"/>
  </xs:complexType>

where the IdentityMode is an enumerated value:

  <xs:simpleType name="IdentityMode">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Identified"/>
      <xs:enumeration value="Indexed"/>
      <xs:enumeration value="None"/>
    </xs:restriction>
  </xs:simpleType>

Here "Identified" signifies a global identifier, and "Indexed" indicates an identifier local only to the parent.

My question is, how do I enforce these uniqueness conditions using unique, key or other schema elements based on the IdentityMode property of the given subtype of ModelElement?

© Stack Overflow or respective owner

Related posts about xml-schema