更改复杂类型中类型的最小限制

时间:2014-04-04 10:25:56

标签: xsd

我有一个名为SafetyTiming的复杂类型,它有2个元素值和边距。值和边距都基于称为Timing的简单类型。

  <xs:simpleType name="Timing">
    <xs:restriction base="xs:unsignedInt">
      <xs:minInclusive value="0" />
      <xs:maxInclusive value="999" />
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="SafetyTiming">
    <xs:sequence>
      <xs:element name="Value" type="Timing" minOccurs="1" maxOccurs="1" />
      <xs:element name="Margin" type="Timing" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
  </xs:complexType>

  <xs:element name="SafetyTimings">
    <xs:complexType>
      <xs:all>
        <xs:element name="A" type="SafetyTiming" minOccurs="1" maxOccurs="1" />
        <xs:element name="B" type="SafetyTiming" minOccurs="1" maxOccurs="1" />
        <xs:element name="C" type="SafetyTiming" minOccurs="1" maxOccurs="1" />
        <xs:element name="D" type="SafetyTiming" minOccurs="1" maxOccurs="1" />
      </xs:all>
    </xs:complexType>
  </xs:element>

这很好,直到我发现元素A和B需要至少为0,因为simpletype&#34; value&#34;并且,C和D至少为1.

我怎样才能优雅地解决这个问题?


我尝试了以下但我认为它看起来相当混乱,我想知道是否有更好的解决方案。

  <!--Safety timing type containing the timing value (minimum 0) and the value margin-->
  <xs:complexType name="SafetyTiming_min0">
    <xs:sequence>
      <xs:element name="Value" type="Timing" minOccurs="1" maxOccurs="1" />
      <xs:element name="Margin" type="Timing" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
  </xs:complexType>

  <!--Safety timing type containing the timing value (minimum 1) and the value margin-->
  <xs:complexType name="SafetyTiming_min1">
    <xs:sequence>
      <xs:element name="Value" minOccurs="1" maxOccurs="1">
        <xs:simpleType>
          <xs:restriction base="Timing">
            <xs:minInclusive value="1" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="Margin" type="Timing" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
  </xs:complexType>

1 个答案:

答案 0 :(得分:0)

您可以从Timing

派生限制类型
<xs:simpleType name="CDTiming">
    <xs:restriction base="Timing">
        <xs:minInclusive value="1" />
    </xs:restriction>
</xs:simpleType>

CD创建具有受限Value的新类型更为简单:

<xs:complexType name="CDSafetyTiming">
    <xs:sequence>
        <xs:element name="Value" type="CDTiming" minOccurs="1" maxOccurs="1" />
        <xs:element name="Margin" type="Timing" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
</xs:complexType>

您不需要为AB更改任何内容,因为原始类型已经至少0

如果您可以更改原始XSD,则可以在元素定义中使用新类型:

<xs:element name="SafetyTimings">
    <xs:complexType>
        <xs:all>
            <xs:element name="A" type="SafetyTiming" minOccurs="1" maxOccurs="1" />
            <xs:element name="B" type="SafetyTiming" minOccurs="1" maxOccurs="1" />
            <xs:element name="C" type="CDSafetyTiming" minOccurs="1" maxOccurs="1" />
            <xs:element name="D" type="CDSafetyTiming" minOccurs="1" maxOccurs="1" />
        </xs:all>
    </xs:complexType>
</xs:element>