2010-11-19 56 views
4

我想的允許在元件的數字位數限制爲6:XSD架構:如何指定值中的位數?

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

字段格式規格是6位,零填充,數字。

i read that i might want to usetotalDigits限制,依據:

totalDigits指定允許數字的確切人數。必須大於零

所以我有簡單類型:

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

什麼是建議限制指定數字的確切位數是多少?

回答

4

您需要使用xs:pattern並提供一個正則表達式來將其限制爲一個數字。

<xs:simpleType name="AccountNumber"> 
    <xs:restriction base="xs:int"> 
     <xs:pattern value="\d{6}"/> 
    </xs:restriction> 
</xs:simpleType> 
+0

,*「你可以使用一種模式與'XS:int'那麼怎麼樣。」 * – 2010-11-19 19:49:20

+1

@Ian:是的,我也有點驚訝。我學到了一些答案。總是樂於提供幫助。 – 2010-11-19 20:19:27

-1

我可能會使用xs:minInclusivexs:maxInclusive

+0

...需要一段時間來通知其他答案已發佈 – Anon 2010-11-19 19:39:13

+0

我相信那些價值的限制,而不是數字的位數。如果minInclusive是7,它意味着值7,而不是7位數。 – 2010-11-19 19:39:40

+1

@Jeff - 你會相信1000000的minInclusive是7位數嗎?而9999999的'maxExclusive'也是七位數字? – Anon 2010-11-20 15:59:37

0

這是我正要說的最簡單方法

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