FindOutIslamNow
FindOutIslamNow

Reputation: 1236

XSD type restriction inheritance?

Suppose I have an attribute type that allows any value of all combinations of (0, 1, 2, 3), namely:

  <xs:simpleType name="fourValues">
    <xs:restriction base="xs:string">
      <xs:enumeration value="0" />
      <xs:enumeration value="1" />
      <xs:enumeration value="2" />
      <xs:enumeration value="3" />
      <xs:enumeration value="0,1" />
      <xs:enumeration value="0,2" />
      <xs:enumeration value="0,3" />
      <xs:enumeration value="0,4" />
      <xs:enumeration value="1,2" />
      <xs:enumeration value="1,3" />
      <xs:enumeration value="2,3" />
      <xs:enumeration value="0,1,2" />
      <xs:enumeration value="0,1,3" />
      <xs:enumeration value="0,2,3" />
      <xs:enumeration value="1,2,3" />
      <xs:enumeration value="0,1,2,3" />
    </xs:restriction>
  </xs:simpleType>

Now what if I want to define a type that is the same as above, but also allowing blank (null) value, like:

  <xs:simpleType name="fourValues-null">
    <xs:restriction base="xs:string">
      <xs:enumeration value="" />
      <xs:enumeration value="0" />
      <xs:enumeration value="1" />
      <xs:enumeration value="2" />
      <xs:enumeration value="3" />
      <xs:enumeration value="0,1" />
      <xs:enumeration value="0,2" />
      <xs:enumeration value="0,3" />
      <xs:enumeration value="0,4" />
      <xs:enumeration value="1,2" />
      <xs:enumeration value="1,3" />
      <xs:enumeration value="2,3" />
      <xs:enumeration value="0,1,2" />
      <xs:enumeration value="0,1,3" />
      <xs:enumeration value="0,2,3" />
      <xs:enumeration value="1,2,3" />
      <xs:enumeration value="0,1,2,3" />
    </xs:restriction>
  </xs:simpleType>

Is there some type of inheritance that avoids me duplication of enumeration for both types?

I have two attributes those use them:

<xs:attribute name="readingSources" type="fourValues" use="required" />
<xs:attribute name="targetFlip" type="fourValues-null" use="required">

I tried to make the targetFlip attributes type="fourValues" and use="optional" but it says that restriction failed.

Upvotes: 0

Views: 408

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

One way to do it is to define the new type that allows extra values as a top-level type, and then redefine your existing type as a restriction of it, with an extra facet such as <minLength value="1"/>.

A second way is to define the new type as a union type of the existing type and a type that only allows a zero length string.

A third way (the one I generally prefer, but it depends whether you are using the schema only for validation or also for data binding) is to define the new type as a list type, with the existing type as its item type, and maxLength set to 1.

Upvotes: 1

Related Questions