2010-09-15 39 views
1

我有下面的XML:極限只有數字字符在可變大小的元素

<customer> 
    <name>John Paul</name> 
    <cpf_cnpj>1376736333334</cpf_cnpj> 
</customer> 

<cpf_cnpj>元件必須具有11的最小尺寸和15的最大尺寸,並且只能有數字(在0和9之間)字符。我的XSD正在尋找這樣的:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="customer"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1" /> 
     <xs:element name="cpf_cnpj" minOccurs="1" maxOccurs="1"> 
      <xs:simpleType> 
      <xs:restriction base="xs:string"> 
       <xs:minLength value="11"/> 
       <xs:maxLength value="15"/> 
       <xs:pattern value="[0-9]{15}"/> 
      </xs:restriction> 
      </xs:simpleType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

我的問題是,xs:pattern節點上,我不知道是幹什麼用的,因爲儘管在我有一個11 minLenght和maxLenght 15,因爲{15},XML不解析是否具有11到14大小的值。
如何獲取11到15之間的變量大小,並僅爲該節點強制使用數字字符?
Tks!

回答

3

嘗試此 <xs:pattern value="[0-9]{11-15}"/>

EDIT

嘗試這個(校正):

<xs:pattern value="[0-9]{11,15}"/> 
1

我只測試了以下圖案

<xs:pattern value="[0-9]+" /> 

和那爲我工作。基本上,使用該模式允許任意長度的數字,然後使用minLength和maxLength字符串來強制執行長度。

因此,在您的示例XSD是:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="customer"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1" /> 
     <xs:element name="cpf_cnpj" minOccurs="1" maxOccurs="1"> 
      <xs:simpleType> 
      <xs:restriction base="xs:string"> 
       <xs:minLength value="11"/> 
       <xs:maxLength value="15"/> 
       <xs:pattern value="[0-9]+"/> 
      </xs:restriction> 
      </xs:simpleType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 
+0

韓國社交協會的人...您的解決方案也工作,但@Topera回答第一。非常感謝您的時間! – Pascal 2010-09-15 23:02:42

相關問題