当前位置 : 主页 > 网页制作 > xml >

xml – XSD架构:如何指定值中的位数?

来源:互联网 收集:自由互联 发布时间:2021-06-13
我想将元素中允许的位数限制为6: AccountNumber123456/AccountNumberAccountNumber999999/AccountNumberAccountNumber000000/AccountNumber 字段格式规范是6位数,零填充,数字. i read that i might want to use totalDigits限制
我想将元素中允许的位数限制为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>
网友评论