我想将元素中允许的数字位数限制为 6:
<AccountNumber>123456</AccountNumber>
<AccountNumber>999999</AccountNumber>
<AccountNumber>000000</AccountNumber>
字段格式规范为 6 位数字,零填充,数字。
i read that i might want to use totalDigits
限制,基于:
totalDigits
Specifies the exact number of digits allowed. Must be greater than zero
所以我有简单的类型:
<xs:simpleType name="AccountNumber">
<xs:restriction base="xs:int">
<xs:totalDigits value="6"/>
</xs:restriction>
</xs:simpleType>
同时它会捕获无效数字,例如:
<AccountNumber>1234567</AccountNumber>
<AccountNumber>0000000</AccountNumber>
<AccountNumber></AccountNumber>
它不会捕获无效数字:
<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>
指定允许的确切位数的建议限制是什么?
最佳答案
您需要使用 xs:pattern
并提供一个正则表达式以将其限制为一个数字。
<xs:simpleType name="AccountNumber">
<xs:restriction base="xs:int">
<xs:pattern value="\d{6}"/>
</xs:restriction>
</xs:simpleType>
关于xml - XSD 架构 : How to specify the number of digits in a value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4228607/