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