XSD scheme: how to indicate the number of digits in a value?

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

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

Field format specification 6-bit, with zero addition, numeric.

I read that I can use a restriction totalDigitsbased on:

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

So I have a 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, for example:

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

it does not detect 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 the recommended limit to indicate the exact number of valid digits?

+5
source share
3 answers

You need to use xs:patternand 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>
+6

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

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

-1
source

All Articles