问题
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?
回答1:
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>
回答2:
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>
回答3:
I would probably use xs:minInclusive and xs:maxInclusive.
来源:https://stackoverflow.com/questions/4228607/xsd-schema-how-to-specify-the-number-of-digits-in-a-value