0

I have the requirement : max. 2 digits - only numbers , is the following code correct:

<xsd:simpleType name="cstCODE">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="[0-9]{2}" />
    <xsd:maxLength value="2" />
  </xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Wiseguy
  • 20,522
  • 8
  • 65
  • 81
user1634274
  • 21
  • 1
  • 1
  • Have you tried it and experienced a problem? – Wiseguy Aug 29 '12 at 21:27
  • no i have not tested yet. but I am a beginner in XSD, that is why because I am asking. – user1634274 Aug 29 '12 at 22:27
  • Without an actual problem, this is kind of a pointless question. Try it out; maybe it's correct. (I don't know XSD, otherwise I'd tell you if it looks okay to me.) If you have a problem with it, _then_ ask the question. :-) – Wiseguy Aug 29 '12 at 22:33
  • how can I test it? ... is there any validation tool? this code is a part of coding writing for an interface ... I only want to test this XSD part, do u have an Idea how? – user1634274 Aug 29 '12 at 22:36
  • There are probably tools that will allow you to check XML input against an XSD. This might help: [XML Schema (XSD) validation tool?](http://stackoverflow.com/questions/124865/xml-schema-xsd-validation-tool) – Wiseguy Aug 29 '12 at 22:41
  • 1
    I find some of the comments you received rather misplaced, but in good spirit, I assume the guys that brought you down to -2 were simply having a bad day. It depends on how much one wants to read through your question and/or spend time to answer it. It is amazing the amount of issues you touched with such a simple question; it shows that people should not rush to label a question "pointless" when (even admitting) not knowing about the subject. If you edit it, I'll upvote again!... – Petru Gardea Aug 31 '12 at 12:27

1 Answers1

3

Short and to the point, your XSD snippet could be correct and here are some things to consider:

Your use of the word max makes me think that less might also be allowed. In which case, your pattern needs maintenance.

<xsd:simpleType name="cstCODE"> 
    <xsd:restriction base="xsd:string"> 
        <xsd:pattern value="[0-9]{1,2}"/> 
        <xsd:maxLength value="2"/> 
    </xsd:restriction> 
</xsd:simpleType> 

It is also interesting (could be unintended) your use of an xsd:string as a base type for what seems to be a numeric value; it is an important distinction in scenarios where one wants to allow empty values on the wire, without using nillable. Then your pattern may look like:

<xsd:simpleType name="cstCODE"> 
    <xsd:restriction base="xsd:string"> 
        <xsd:pattern value="[0-9]{0,2}"/> 
        <xsd:maxLength value="2"/> 
    </xsd:restriction> 
</xsd:simpleType> 

Another problem here might be that related to positive/negative numbers, and/or 0 padding. This is where the patterns can get a bit more complicated.

If your use of xsd:string as a base type is rather part of the learning curve, then you could achieve the same "range" limitation using a numeric base type, and the facets that come with it. Once in a while I revisit this chart; in there you can choose a type that matches your needs, click on it, and see what constraining facets apply to it. For, e.g., if I choose an unsignedShort then:

<xsd:simpleType name="cstCODE"> 
    <xsd:restriction base="xsd:unsignedShort"> 
        <xsd:totalDigits value="2"/> 
    </xsd:restriction> 
</xsd:simpleType> 
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • I think using a numeric base type would not apply here given that it allows -- in the case of the unsignedShort -- a + sign as a prefix. –  Mar 30 '17 at 13:31