开发者

Limit only numeric chars in XSD in variable sized element

I have the following XML:

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

The <cpf_cnpj> element must have a minimum size of 11 and a maximum size of 15, and there can only be numeric (between 0 and 9) characters. My XSD is looking like this:

<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">
          开发者_JS百科<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>

My problem is that on the xs:pattern node, I don't know what to use, because despite the fact the I have a minLenght of 11 and maxLenght of 15, because of the {15}, the XML does not parse if a have an 11 to 14 sized value.

How can I have a variable size ranging from 11 to 15, and enforce only numeric characters for this node?

Tks!


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

EDIT

Try this (corrected):

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


I just tested the following pattern

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

and that worked for me. Basically, use the pattern to allow any length of numbers, and then use minLength and maxLength strings to enforce length.

So the XSD in your example would be:

<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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜