How can allowed element values depend on other element values in XSD?

北战南征 提交于 2020-01-05 04:54:13

问题


I have a following task (in simple version): Describe in XSD temperatures. There are min, max and default values. Default should be placed inside of min...max values. How to describe these things with XSD?

I mean:

Min_Set_Temp [ Integer between 0 and 1000 ]
Max Set Temp [ Integer between 0 and 1000 ]
Default_Set_Temp [ Integer between Min Set Temp and Max Set Temp]

XSD start:

<xs:simpleType name="tSetTemperature">
    <xs:restriction base="xs:unsignedShort">
        <xs:minInclusive value="0"/>
        <xs:maxInclusive value="1000"/>
    </xs:restriction>
</xs:simpleType>

<xs:element name="Bake">
  <xs:complexType>
    <xs:sequence>
        <xs:element name="MinSetTemp" type="tSetTemperature"/>
        <xs:element name="MaxSetTemp" type="tSetTemperature"/>
        <xs:element name="DefaultTemp" type="tSetTemperature"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

回答1:


XSD 1.0

Not possible.

XSD 1.1

Possible using xs:assert:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">

  <xs:simpleType name="tSetTemperature">
    <xs:restriction base="xs:unsignedShort">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="1000"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="Bake">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="MinSetTemp" type="tSetTemperature"/>
        <xs:element name="MaxSetTemp" type="tSetTemperature"/>
        <xs:element name="DefaultTemp" type="tSetTemperature"/>
      </xs:sequence>
      <xs:assert test="DefaultTemp = MinSetTemp to MaxSetTemp"/>
    </xs:complexType>
  </xs:element>
 </xs:schema>


来源:https://stackoverflow.com/questions/40343179/how-can-allowed-element-values-depend-on-other-element-values-in-xsd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!