5

i want to limit the number of digits allowed in an element to 6:

<AccountNumber>123456</AccountNumber>
<AccountNumber>999999</AccountNumber>
<AccountNumber>000000</AccountNumber>

The field format specification is 6-digit, zero-padded, numeric.

i read that i might want to use the totalDigits restriction, based on:

totalDigits Specifies the exact number of digits allowed. Must be greater than zero

So i have the simple type:

<xs:simpleType name="AccountNumber">
   <xs:restriction base="xs:int">
      <xs:totalDigits value="6"/>
   </xs:restriction>
</xs:simpleType>

And while it catches invalid numbers, such as:

<AccountNumber>1234567</AccountNumber>
<AccountNumber>0000000</AccountNumber>
<AccountNumber></AccountNumber>

it doesn't catch invalid numbers:

<AccountNumber>12345</AccountNumber>
<AccountNumber>01234</AccountNumber>
<AccountNumber>00123</AccountNumber>
<AccountNumber>00012</AccountNumber>
<AccountNumber>00001</AccountNumber>
<AccountNumber>00000</AccountNumber>
<AccountNumber>0000</AccountNumber>
<AccountNumber>000</AccountNumber>
<AccountNumber>00</AccountNumber>
<AccountNumber>0</AccountNumber>

What is a suggested restriction to specify the exact number of digits allowed?

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

3 Answers3

7

You need to use xs:pattern and provide a regular expression to limit it to a number.

<xs:simpleType name="AccountNumber">
   <xs:restriction base="xs:int">
      <xs:pattern value="\d{6}"/>
   </xs:restriction>
</xs:simpleType>
Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
0

This is the simplest way

    <xs:element name="prodid">
     <xs:simpleType>
      <xs:restriction base="xs:integer">
       <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
      </xs:restriction>
     </xs:simpleType>
    </xs:element> 
Artur
  • 1
0

I would probably use xs:minInclusive and xs:maxInclusive.

Anon
  • 2,654
  • 16
  • 10
  • ... takes SO a while to notify that other answers have been posted – Anon Nov 19 '10 at 19:39
  • I believe those limit by value not by number of digits. As in if minInclusive is 7 it means the value 7, not 7 digits. – Jeff Yates Nov 19 '10 at 19:39
  • 1
    @Jeff - would you believe that `minInclusive` of 1000000 is seven digits? And that `maxExclusive` of 9999999 is also seven digits? – Anon Nov 20 '10 at 15:59
  • @Anon: It's a bit of a dodgy way to enforce the length. Nice attitude though. ;) – Jeff Yates Nov 21 '10 at 20:30
  • @Jeff - I don't think it's dodgy, as long as the type is `xs:int`. Rereading the question however, the OP wants to preserve leading zeroes, which says to me that the type should really be `xs:string`. – Anon Nov 29 '10 at 15:04
  • @Anon: We're entitled to our opinions, but I very strongly feel that this is a hack rather than the best way to achieve the goal. Especially since the right way is so easy. – Jeff Yates Nov 29 '10 at 15:21
  • 1
    @Jeff - :shrug: hacks are where you find them. For me, constraining *integer values* by anything other than a *numeric limit* is a hack. *String* values, on the other hand, are appropriately constrained by regexes. My point is that the OP's data type is the issue. – Anon Nov 29 '10 at 15:47
  • @Anon: I clearly disagree. The data type is exactly right if they want these values to be interpreted as integers and enforcing a length with a pattern seems right in that situation. Enforcing length by constraining min and max values is a workaround. I don't believe the OP has got their data type wrong here, the data type just needs additional constraints to fit the use-case and these are provided by using a pattern. – Jeff Yates Nov 29 '10 at 17:54