【问题标题】:Apply restriction to time attribute in XML Schema对 XML Schema 中的时间属性应用限制
【发布时间】:2015-04-21 18:27:06
【问题描述】:

我想对 XML Schema 应用特定限制(我在这方面经验很少)。

我有一个xsd:time类型的属性:

<xsd:attribute name="hour" type="xsd:time" use="required"/>

我想要的是应用一个限制,以便 XML 仅在半小时的时间间隔内有效。例如,10:00, 12:30, 15:30, 20:00 将是小时属性的有效值,但 10:45, 11:12, 15:34 em> 等不会。

我怎样才能做到这一点?我的搜索没有给出有用的东西。

提前谢谢你。

【问题讨论】:

    标签: xml xsd schema restriction


    【解决方案1】:

    你可以这样定义你的时间。

    <xsd:attribute name="hour" type="Time" use="required"/>
    
    <xsd:simpleType name="Time">
        <xsd:restriction base="xsd:time">
            <xsd:enumeration value="00:00:00"/>
            <xsd:enumeration value="00:30:00"/>
            <xsd:enumeration value="01:00:00"/>
            <xsd:enumeration value="01:30:00"/>
            <xsd:enumeration value="02:00:00"/>
            <xsd:enumeration value="02:30:00"/>
            <xsd:enumeration value="03:00:00"/>
            <xsd:enumeration value="03:30:00"/>
            <!-- etc etc -->
        </xsd:restriction>
    </xsd:simpleType>
    

    <xsd:simpleType name="Time">
        <xsd:restriction base="xsd:time">
            <xsd:pattern value="((0[0-9]|1[0-9]|2[0-3]):[0|3][0]:[0][0])"/>
        </xsd:restriction>
    </xsd:simpleType>
    

    【讨论】:

    • base 的值应以 xsd 为前缀。另外我认为使用这个更简单的正则表达式它仍然可以工作..:(0|3)0:00
    • 您能提供一个答案吗?这样会很好! :)
    • @sergioFC 我曾想过简化表达式,但是,您的解决方案允许这些值 24:00:00、00:00:0,我认为这有点模棱两可。无论如何,两种解决方案都是正确的,并且缺少xs:time我的错:)
    • 你说得对,我没有注意到我评论的解决方案同时验证了 00:00:00 和丑陋的 24:00:00。
    猜你喜欢
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 2020-07-19
    • 1970-01-01
    相关资源
    最近更新 更多