2010-06-10 51 views
1

我這是在XML模式作爲類型RangeType如何相互

<complexType name="RangeType"> 
    <attribute name="min" use="required" type="double" /> 
    <attribute name="max" use="required" type="double" /> 
</complexType> 

描述像

<Range min="-5.0" max="5.0" /> 

一個範圍元素是否可以使用XML-Schema來比較屬性要求max屬性大於min屬性?

回答

1

不可以。您不能在XML模式中指定交叉元素(編輯:或跨屬性)約束。

您將不得不編寫代碼或使用類似Schematron的東西。

+0

好的,我很害怕那樣。謝謝。 – mstahlberg 2010-06-11 06:48:58

0

爲了將來的參考,一種解決方案可能是以不同的方式定義你的範圍,用'開始'和'計數'來代替最小和最大值。

所以,你的例子可以寫成:

<Range start="-5.0" count="10.0" /> <!-- range from -5 to 5 --> 

然後,您可以使用模式到count限制到0.0的最低值,使其無法計算的最大價值比最低金額:

<xs:complexType name="RangeType"> 
    <xs:attribute name="start" use="required" type="xs:double" /> 
    <xs:attribute name="count" use="required"> 
    <xs:simpleType> 
     <xs:restriction base="xs:double"> 
     <xs:minInclusive value="0.0"/> 
     </xs:restriction> 
    </xs:simpleType> 
    </xs:attribute> 
</xs:complexType> 

如果您不需要的範圍是雙,你也可以只定義counttype="unsignedInt",這將避免自定義類型。