2

This is my first question on stackoverflow (I have usually been able to figure out my problems with the answers already here).

This is similar to these questions, but I think my question is a little different (also, XSD is really hard):

I am trying to define a restricted set of "types" that represent parameter data types. E.g.

    - Binary bit-mapped    B/BM    1 byte
    - Unsigned Integer     U/I     2 bytes
    - Alphanumeric         ALPHA   1 byte
    - etc...

So I have come up with the following xsd:

<xs:complexType name="parameterDataType" abstract="true">
    <xs:sequence>
        <xs:element name="type" type="xs:string" />
        <xs:element name="abbreviation" type="xs:string" />
        <xs:element name="length" type="xs:positiveInteger" />
    </xs:sequence>
</xs:complexType>

<xs:complexType name="binaryBitMappedType">
    <xs:complexContent>
        <xs:restriction base="parameterDataType">
            <xs:sequence>
                <xs:element name="type" type="xs:string" fixed="Binary Bit-Mapped" />
                <xs:element name="abbreviation" type="xs:string" fixed="B/BM" />
                <xs:element name="length" type="xs:positiveInteger" fixed="1" />
            </xs:sequence>
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>
etc...

NOTE WELL: I also tried this with a sequence and "type, abbreviation, length" being xs:attributes, so if that's a better solution let me know.

This works fine but it requires the XML to redefine the element as their restricted values (otherwise it is not valid), a bit like so:

<message>
    <id>123</id>
    <name>Some status type message</name>
    <dataType xsi:type="binaryBitMappedType">
        <type>Binary Bit-Mapped</type>
        <abbreviation>B/BM</abbreviation>
        <length>1</length>
    </dataType>
    ...etc
</message>
<message>
    <id>321</id>
    <name>A DIFFERENT status type message</name>
    <dataType xsi:type="binaryBitMappedType">
        <type>Binary Bit-Mapped</type>
        <abbreviation>B/BM</abbreviation>
        <length>1</length>
    </dataType>
    ...etc
</message>
<message>
    <id>456</id>
    <name>a continuous value type message</name>
    <dataType xsi:type="unsignedIntegerType">
        <type>Unsigned Integer</type>
        <abbreviation>U/I</abbreviation>
        <length>2</length>
    </dataType>
    ...etc
</message>

Is there a way to avoid having to redefine these values in the XML (can the definition be pulled in from the XSD, or assumed from the XSD?) Alternatively, can the definitions be provided in their own XML file and then those be "imported" in based on the xsi:type?

Something a little more like this?

<message>
    <id>123</id>
    <name>Some status type message</name>
    <dataType xsi:type="binaryBitMappedType" /> <!-- don't have to repeat the dataType contents -->
    ...etc
</message>
<message>
    <id>321</id>
    <name>A DIFFERENT status type message</name>
    <dataType xsi:type="binaryBitMappedType" /> <!-- don't have to repeat the dataType contents -->
    ...etc
</message>

Or am I on a hiding to nothing? Thanks in advance

Community
  • 1
  • 1
Doddie
  • 1,393
  • 13
  • 21

1 Answers1

1

The general rule in XSD 1.0 is that you can't define co-constraints: that is, the valid values of one element are entirely independent of the valid values of any other element.

In XSD 1.1 you can do what you like, using assertions.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Hi, thanks for the answer. I didn't know about assertions and they look like they might solve some of this problem. I have extended the question a little to try and illustrate the parts that I am trying to avoid repeating. – Doddie Feb 02 '12 at 02:39