5

I want to apply a specific restriction to an XML Schema (on which I have very little experience).

I have an attribute of xsd:time type:

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

What I want to to is apply a restriction so that the XML will be valid only on half-hour time intervals. For example, 10:00, 12:30, 15:30, 20:00 would be valid values for the hour attribute but 10:45, 11:12, 15:34 etc would not.

How can I achieve this? My search did not give out something useful.

Thank you in advance.

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54

1 Answers1

3

You could define your time in this way.

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

or

<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>
Xstian
  • 8,184
  • 10
  • 42
  • 72
  • 2
    The value of base should be prefixed with xsd. In addition I think that using this simplier regex it would still work `..:(0|3)0:00` – sergioFC Apr 18 '15 at 23:56
  • Could you please provide an answer on this? It will be nice! :) – Nick Louloudakis Apr 20 '15 at 22:52
  • @sergioFC I had thought to simplify the expression but, your solution allows these value 24:00:00, 00:00:0, I think that is a little ambiguous. Anyway both solution are right, and missing `xs:time` my fault :) – Xstian Apr 21 '15 at 10:26
  • You're rigth, I haven't noticed the solution of my comment validates both 00:00:00 and the ugly 24:00:00. – sergioFC Apr 21 '15 at 11:28