0
votes

I am getting an error saying '' is not a valid value for 'integer'. I have tried using nillable="true" for the element and tried changing the pattern values using the restriction. The parentInstanceId element in the xml file doesn't have any values. But I am supposed to give a restriction of 20 characters to it.

This is my xsd file.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="createSalesOrder">          <!--root-->
  <xs:complexType mixed="true">
   <xs:sequence>
    <xs:element name="order">                  <!--sub-root-->
     <xs:complexType mixed="true"> 
      <xs:sequence>

       <xs:element name="orderSource">       <!--orderSource-->
        <xs:simpleType>
        <xs:restriction base="xs:string">
        <xs:maxLength value="30"/>
        </xs:restriction>
        </xs:simpleType>
       </xs:element>

       <xs:element name="purchaseOrder">       <!--purchaseOrder-->
       <xs:simpleType>
    <xs:restriction base="xs:string">
        <xs:maxLength value="20"/>
        </xs:restriction>
        </xs:simpleType>
       </xs:element>

       <xs:element name="purchaseOrderType">   <!--purchaseOrderType-->
       <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:maxLength value="5"/>
        </xs:restriction>
        </xs:simpleType>
       </xs:element>

       <xs:element name="purchaseOrderDate">   <!--purchaseOrderDate-->
       <xs:simpleType>
    <xs:restriction base="xs:date"/>
        </xs:simpleType>
       </xs:element>

        <xs:element name="orderType">         <!--orderType-->
       <xs:simpleType>
    <xs:restriction base="xs:string">
        <xs:maxLength value="10"/>
        </xs:restriction>
        </xs:simpleType>
       </xs:element>

       <xs:element name="orderExpiry">             <!--orderExpiry-->
       <xs:simpleType>
    <xs:restriction base="xs:integer">
        <xs:minInclusive value="1"/>
        <xs:maxInclusive value="5"/>
        </xs:restriction>
        </xs:simpleType>
       </xs:element>

       <xs:element name="orderConsumerReferenceId">  <!--OCRId-->
       <xs:simpleType>
    <xs:restriction base="xs:string">
        <xs:maxLength value="20"/>
        </xs:restriction>
        </xs:simpleType>
       </xs:element>

       <xs:element name="orderSourceReferenceId">  <!--OSRId-->
       <xs:simpleType>
        <xs:restriction base="xs:integer">
        <xs:pattern value="\d{7}"/>
        </xs:restriction>
        </xs:simpleType>
       </xs:element>

       <xs:element name="shipping">       <!--shipping-->
        <xs:complexType mixed="true">
         <xs:sequence>
          <xs:element name="requestedDate" type="xs:dateTime"/>
          <xs:element name="shipmentOrigin" type="xs:boolean"/>
         </xs:sequence>
        </xs:complexType>
       </xs:element>

       <xs:element name="orderCurrency">  <!--orderCurrency-->
        <xs:complexType mixed="true">
         <xs:sequence>
          <xs:element name="currencyCode" type="xs:string" default="EUR"/>
          <xs:element name="currencyExchangeRate">
           <xs:simpleType>
            <xs:restriction base="xs:integer">
             <xs:enumeration value="1"/>
            </xs:restriction>
           </xs:simpleType>
          </xs:element>
         </xs:sequence>
        </xs:complexType>
       </xs:element>

       <xs:element name="partner" maxOccurs="unbounded">  <!--partner-->
        <xs:complexType mixed="true">
         <xs:sequence>
          <xs:element name="partnerRole" type="xs:string"/>
          <xs:element name="partnerId"/>
          <xs:element name="communicationLanguageCode" type="xs:string"/>
          <xs:element name="address" minOccurs="0" maxOccurs="unbounded">
           <xs:complexType mixed="true">
            <xs:sequence>
              <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1"/>
             <xs:element name="value">
             <xs:simpleType>
             <xs:restriction base="xs:string">
              <xs:maxLength value="50"/>
             </xs:restriction>
             </xs:simpleType>
             </xs:element>
            </xs:sequence>
           </xs:complexType>
          </xs:element>
         </xs:sequence>
        </xs:complexType>
       </xs:element>

       <xs:element name="salesOrganization">  <!-- salesOrganization -->
        <xs:simpleType>
         <xs:restriction base="xs:string">
          <xs:maxLength value="10"/>
         </xs:restriction>
        </xs:simpleType>
       </xs:element>

       <xs:element name="distributionChannel"> <!--distributionChannel-->
        <xs:simpleType>
         <xs:restriction base="xs:string">
          <xs:pattern value="[A-Z][A-Z]"/>
         </xs:restriction>
        </xs:simpleType>
       </xs:element>

       <xs:element name="division" type="xs:integer"/>
       <xs:element name="orderTerm" minOccurs="1" maxOccurs="unbounded"> <!--orderTerm-->
        <xs:complexType mixed="true">
         <xs:sequence>
          <xs:element name="name" type="xs:string"/>
          <xs:element name="value">
           <xs:simpleType>
            <xs:restriction base="xs:string">
             <xs:enumeration value="2 YEARS"/>
             <xs:enumeration value="3 YEARS"/>
             <xs:enumeration value="4 YEARS"/>
             <xs:enumeration value="5 YEARS"/>
            </xs:restriction>
           </xs:simpleType>
          </xs:element>
         </xs:sequence>
        </xs:complexType>
       </xs:element>
       <xs:element name="lineItem" minOccurs="0">  <!--lineItem-->
        <xs:complexType mixed="true">
         <xs:sequence>
          <xs:element name="lineId" type="xs:integer"/>
          <xs:element name="parentLineId">      <!--parentId-->
           <xs:simpleType>
            <xs:restriction base="xs:string">
             <xs:maxLength value="12"/>
            </xs:restriction>
           </xs:simpleType>
          </xs:element>
          <xs:element name="materialFlowType" type="xs:integer"/>
          <xs:element name="material" minOccurs="1" type="xs:string"/>
          <xs:element name="materialDescription" minOccurs="0">  <!--materialDescription-->
           <xs:simpleType>
            <xs:restriction base="xs:string">
             <xs:maxLength value="20"/>
            </xs:restriction>
           </xs:simpleType>
          </xs:element>
          <xs:element name="orderedQuantity" minOccurs="1" type="xs:integer"/> 
          <xs:element name="configuration">     <!--configuration-->
           <xs:complexType mixed="true">
            <xs:sequence>
             <xs:element name="instanceId">      <!--instanceId-->
              <xs:simpleType>
               <xs:restriction base="xs:integer">
                <xs:pattern value="\d{1,20}"/>
               </xs:restriction>
              </xs:simpleType>
             </xs:element>
             <xs:element name="parentInstanceId" nillable="false">  <!--parentInstanceId-->
              <xs:simpleType>
               <xs:restriction base="xs:integer">
                <xs:pattern value="[0-9]{0,20}"/>
               </xs:restriction>
              </xs:simpleType>
             </xs:element>
             <xs:element name="quantity" type="xs:integer"/>
             <xs:element name="objectKey">        <!--objectKey-->
              <xs:simpleType>
               <xs:restriction base="xs:string">
                <xs:maxLength value="25"/>
               </xs:restriction>
              </xs:simpleType>
             </xs:element>
             <xs:element name="characteristics" maxOccurs="unbounded">  <!--characteristics-->
              <xs:complexType mixed="true">
               <xs:sequence>
                <xs:element name="type" minOccurs="1">
                 <xs:simpleType>
                  <xs:restriction base="xs:string">
                   <xs:maxLength value="15"/>
                  </xs:restriction>
                 </xs:simpleType>
                </xs:element>
                <xs:element name="value" minOccurs="1">
                 <xs:simpleType>
                  <xs:restriction base="xs:string">
                   <xs:maxLength value="20"/>
                  </xs:restriction>
                 </xs:simpleType>
                </xs:element>
               </xs:sequence>
              </xs:complexType>
             </xs:element>                                  <!--characteristics ends-->
            </xs:sequence>
           </xs:complexType>
          </xs:element>              <!--configuration ends-->
          <xs:element name="price">       <!--price-->
           <xs:complexType mixed="true">
            <xs:sequence>
             <xs:element name="name">      <!--name-->
              <xs:simpleType>
               <xs:restriction base="xs:string">
                <xs:maxLength value="15"/>
               </xs:restriction>
              </xs:simpleType>
             </xs:element>
             <xs:element name="value" type="xs:decimal"/>
             <xs:element name="currencyCode" default="EUR" minOccurs="1" maxOccurs="1">
              <xs:simpleType>
               <xs:restriction base="xs:string">
                <xs:maxLength value="3"/>
               </xs:restriction>
              </xs:simpleType>
             </xs:element>
            </xs:sequence>
           </xs:complexType>
          </xs:element>                       <!--price ends-->
          <xs:element name="shippingDate">     <!--shippingDate-->
           <xs:complexType mixed="true">
            <xs:sequence>
             <xs:element name="name">
              <xs:simpleType>
               <xs:restriction base="xs:string">
                <xs:maxLength value="15"/>
               </xs:restriction>
              </xs:simpleType>
             </xs:element>
             <xs:element name="value" type="xs:dateTime"/>
            </xs:sequence>
           </xs:complexType>
          </xs:element>                         <!--shippingDate ends-->
          <xs:element name="shipmentOrigin" type="xs:string"/>
         </xs:sequence>
        </xs:complexType>
       </xs:element>                                   <!--lineItem ends-->
       <xs:element name="callbackUrl" type="xs:string"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element> 
    </xs:sequence>
   </xs:complexType>
  </xs:element>
 </xs:schema>   

and this the xml file for which I am supposed to develop the xsd file

<createSalesOrder>
   (M)
   <order>
      (M)
      <orderSource>BridgeSAP</orderSource>
      //restriction of 30 characters (M)
      <purchaseOrder>test USO now</purchaseOrder>
      //restriction of 20 characters (M)
      <purchaseOrderType>ZSATA</purchaseOrderType>
      //restriction of 5 characters, refer from value (M)
      <purchaseOrderDate>2015-06-25</purchaseOrderDate>
      //restriction as of the value (M)
      <orderType>ZOR</orderType>
      //restriction of 10 characters (O)
      <orderExpiry>2</orderExpiry>
      //restriction of expiry ranges from 1-5
      <orderConsumerReferenceId>order017</orderConsumerReferenceId>
      //restriction of 20 characters (M)
      <orderSourceReferenceId>6003670</orderSourceReferenceId>
      //restriction of 8 numbers (M)
      <shipping>
         <requestedDate>2015-10-30T15:12:25</requestedDate>
         //restriction as of the value (M)
         <shipmentOrigin>true</shipmentOrigin>
         //restriction as of the value (O)
      </shipping>
      <orderCurrency>
         <currencyCode>EUR</currencyCode>
         //default the currency to EUR and provide restrictions (M)
         <currencyExchangeRate>1</currencyExchangeRate>
         //restriction as of the value (M)
      </orderCurrency>
      <partner>
         <partnerRole>AG</partnerRole>
         //restriction as of the value (M)
         <partnerId>CFR01440</partnerId>
         //restriction as of the value (M)
         <communicationLanguageCode>fr</communicationLanguageCode>
         //restriction as of the value (O)
      </partner>
      <partner>
         <partnerRole>WE</partnerRole>
         <partnerId/>
         <communicationLanguageCode>ag</communicationLanguageCode>
         <address>
            (O)
            <name>NAME1</name>
            //restriction of 10 characters (M)
            <value>Example Name 1</value>
            //restriction of 50 characters (O)
         </address>
         <address>
            <name>NAME2</name>
            <value>Example Name 2</value>
         </address>
      </partner>
      <salesOrganization>FR08</salesOrganization>
      //restriction of 10 characters (M)
      <distributionChannel>IG</distributionChannel>
      //restriction as of the value (M)
      <division>01</division>
      //restriction as of the value (M)
      <orderTerm>
         (M)
         <name>INCO1</name>
         //restriction as of the value (M)
         <value>2 YEARS</value>
         //restriction as of enumeration (2 YEARS, 3 YEARS, 4 YEARS, 5 YEARS) (M)
      </orderTerm>
      <orderTerm>
         <name>INCO2</name>
         <value>3 YEARS</value>
      </orderTerm>
      <orderTerm>
         <name>ZTERM</name>
         <value>4 YEARS</value>
      </orderTerm>
      <lineItem>
         (O)
         <lineId>1000</lineId>
         //restriction as of the value (M)
         <parentLineId/>
         //restriction of 12 characters (O)
         <materialFlowType>1</materialFlowType>
         //restriction as of the value (M)
         <material>GCR_S24SWB_01</material>
         //restriction as of the value (M)
         <materialDescription>SM6 24kV</materialDescription>
         //restriction of 20 characters (O)
         <orderedQuantity>1</orderedQuantity>
         //restriction as of the value (M)
         <configuration>
            <instanceId>00000001</instanceId>
            //restriction as of 20 characters (M)
            <parentInstanceId/>
            //restriction as of 20 characters (O)
            <quantity>1</quantity>
            //restriction as of the value (M)
            <objectKey>GCR_S24SWB_01</objectKey>
            //restriction as of 25 characters (M)
            <characteristics>
               <type>PRO_ARKTX1</type>
               //restriction as of 15 characters (M)
               <value>SM6 Test</value>
               //restriction as of 20 characters (M)
            </characteristics>
            <characteristics>
               <type>PRO_ETO_FLAG</type>
               <value>1</value>
            </characteristics>
         </configuration>
         <price>
            <name>ZPN0</name>
            //restriction as of 15 characters (M)
            <value>4500.50</value>
            //restriction as of the value (M)
            <currencyCode>EUR</currencyCode>
            //default the currency to EUR and provide restrictions (M)
         </price>
         <shippingDate>
            <name>ETDAT</name>
            //restriction as of 15 characters (M)
            <value>2015-10-30T15:12:25</value>
            //restriction as of the value (M)
         </shippingDate>
         <shipmentOrigin>FR12</shipmentOrigin>
         //restriction as of the value (O)
      </lineItem>
      <callbackUrl>https://www.example.com/xml</callbackUrl>
   </order>
</createSalesOrder>
2

2 Answers

2
votes

In the XML document you would need <parentInstanceId xsi:nil="true"/> to indicate there is no value, see https://www.w3.org/TR/xmlschema-0/#Nils for a complete example.

2
votes

There are two ways to define a simpleType that accepts either an integer, or nothing (a zero-length string):

(a) define a union type with two member types, one being xs:integer (or a subtype thereof), the other being a restriction of xs:string with length="0".

(b) define a list type with item type xs:integer and with maxLength="1".

The two approaches are equivalent as far as validating the XML is concerned, but they are likely to behave differently with data binding technologies, or with schema-aware XSLT and XQuery.