views:

19

answers:

2

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"&gt;
  <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>

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!

+1  A: 

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

EDIT

Try this (corrected):

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

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"&gt;
  <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>
Dunderklumpen
Tks man... your solution worked also, but @Topera answered first. Thanks so much for your time!
Pascal