【问题标题】:xml schema constraints integer incrementxml 模式约束整数增量
【发布时间】:2016-09-01 17:12:15
【问题描述】:

我想编写一个带有整数约束的 XML 模式。整数应该只在一定的增量下才有效。

基本元素可能是这样的:

 <xs:element name="MotorFrequency">
        <xs:simpleType>
            <xs:restriction base="xs:integer">
                <xs:minInclusive value="1" />
                <xs:maxInclusive value="2000000" />
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

我需要一个仅允许增量为 4000 的整数的约束,例如 4000、8000、12000 等。

我怎样才能做到这一点?

【问题讨论】:

    标签: xml xsd integer constraints schema


    【解决方案1】:

    只有 XSD 1.1,您可以使用 &lt;xs:assertion&gt;:

    <xs:element name="MotorFrequency">
        <xs:simpleType>
            <xs:restriction base="xs:integer">
                <xs:minInclusive value="1" />
                <xs:maxInclusive value="2000000" />
                <xs:assertion test="($value mod 4000) = 0"></xs:assertion>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
    

    这样,如果断言失败,就会引发错误。

    【讨论】:

      【解决方案2】:

      如果你被 XSD 1.0 卡住了,那么你可以写一个正则表达式

      value="[0-9]*(([02468][048])|([13579][26])000"
      

      未测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-30
        • 2021-08-03
        • 1970-01-01
        • 2014-07-28
        • 1970-01-01
        相关资源
        最近更新 更多