开发者

How to compare attributes with each other

I have a Range element like

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

which is described in an XML schema开发者_如何转开发 as the type RangeType

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

Is it possible to use XML-Schema to require the max attribute to be greater than the min attribute?


No. You cannot specify cross-element (edit: or cross-attribute) constraints in XML Schema.

You will have to write code or use something like Schematron.


For future reference, one solution might be to define your range differently, with a 'start' and a 'count' instead of a minimum and maximum.

So your example could be rewritten as:

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

You can then use the schema to restrict the count to a minimum value of 0.0, making it impossible for the calculated maximum value to be lower than the minimum:

<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>

If you don't need the range to be a double, you could also just define count as being type="unsignedInt", which would avoid the custom type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜