XSD 允许减半

XSD allow double by half

我想对 xsd:complexType 进行限制,其中仅包含 0.520 之间的值,两者都包括在内,步骤为 0.5。这意味着数字:

0.5    1   1.5   2   2.5   3   3.5  .. up to 20

我的代码在这里:

<xsd:complexType name="skillType">
    <xsd:simpleContent>
        <xsd:extension base="skillHalfDoubleType">
            <xsd:attribute name="special">
                <!-- Irrelevant attribute -->
            </xsd:attribute>
        </xsd:extension>
    </xsd:simpleContent>
</xsd:complexType>

<xsd:simpleType name="skillHalfDoubleType">
    <xsd:restriction base="xsd:double">
         <!-- What more? -->
    </xsd:restriction>
</xsd:simpleType>

可以通过在 xsd:string 上应用正则表达式来做到这一点,但是我正在寻找限制 xsd:double.

的解决方案

一个简单(虽然冗长)的解决方案是使用枚举:

<xs:enumerationvalue="0.5"/>
<xs:enumerationvalue="1"/>
<xs:enumerationvalue="1.5"/>
...

一个更优雅的解决方案是使用结合了 min 和 maxInclusive 的模式:

<xs:minInclusivevalue="0"/>
<xs:maxInclusivevalue="20"/>
<xs:pattern value="([1-9]?[0-9])|([1-9]?[0-9].5)"/>

正如 C. M. Sperberg-McQueen 所指出的,后者允许更容易的范围适应,但可能会使一些模式感知建议提供者感到困惑。

使用 XML Schema 1.1,您可以编写一个限制值的断言

<xs:simpleType name="skillHalfDoubleType">
    <xs:restriction base="xs:double">
        <xs:assertion test="$value = (for $d in 1 to 40 return 0.5 * $d)"></xs:assertion>
    </xs:restriction>
</xs:simpleType>