Best practice to handle xml with empty integer values

0
I have another party that has supplied an XML. <?xml version="1.0" encoding="UTF-8"?> <Report>   <Header>     <GeneratedBy>Some User</GeneratedBy>   </Header>   <Order>     <OrderNumber>1</OrderNumber>   </Order>   <Order>     <OrderNumber>2</OrderNumber>   </Order> </Report> Because of the way Mendix would like the XML (Mendix prefers <Orders><Order>...</Order><Order>...</Order></Orders>), we need to supply an xsd in order to create an Import Mapping that will read this XML (the other party doesn’t have one). <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">   <xs:element name="Report">     <xs:complexType>       <xs:sequence>         <xs:element name="Header">           <xs:complexType>             <xs:sequence>               <xs:element name="GeneratedBy" type="xs:string" />             </xs:sequence>           </xs:complexType>         </xs:element>         <xs:element minOccurs="0" maxOccurs="unbounded" name="Order">           <xs:complexType>             <xs:sequence>               <xs:element name="OrderNumber" minOccurs="0" maxOccurs="1" type="xs:integer" />             </xs:sequence>           </xs:complexType>         </xs:element>       </xs:sequence>     </xs:complexType>   </xs:element> </xs:schema> This works fine as long as the other party sends integer values in the element <OrderNumber>. However, when they don’t have an order number (empty value), instead of not sending the element (which we can handle), they send an empty element <OrderNumber />. This breaks the validation with the xsd, as it expects an integer, not an empty string ‘’. One way to handle this, is to extend the xsd with a new type:   <xs:simpleType name="emptyOrInteger">     <xs:restriction base="xs:string">         <xs:pattern value="-?\d*"/>     </xs:restriction>   </xs:simpleType> This accepts a string with the following pattern: (zero or one – (to create negative numbers)) and zero or more digits (0-9). This would effectively allow for a string to be composed of only positive or negative numbers (or be an empty value). Another way is to introduce a union type: <xs:simpleType name="UnionInteger">     <xs:union>       <xs:simpleType>         <xs:restriction base='xs:integer' />       </xs:simpleType>       <xs:simpleType>         <xs:restriction base='xs:string'>           <xs:length value="0"/>         </xs:restriction>       </xs:simpleType>     </xs:union>   </xs:simpleType> So basically either an integer or a string with length 0. Our domain model has the attribute mapped as integer. As there doesn’t seem to be a way to create an xsd that defined an integer and accepts an empty value <OrderNumber /> and an integer <OrderNumber>1</OrderNumber>, we feed every incoming OrderNumber through a microflow to either parse the string as integer or supply an empty value. So what are the best practices (other than forcing the customer to remove empty integer values)? Remove all empty tags before applying the import mapping (so it will be compatible with the xsd and value being an integer) Use the flow as described above (allow an empty string or a number in the element and transform the string value to an integer value Use XSL transformation to remove empty integer values (if that’s possible) Another clever trick?
asked
2 answers
0

Have you tried nillable=”true” to accept empty values?

answered
0

In the importmapping add a conversion microflow to the integer-attribute setting the value to empty when ‘’ is received.

*Edited after your response*

Ah, got it. Your the xsd-definition to accept the value as an integer and also as empty, but that means a string. Ok, use union to do this:

<xs:element name="job_code">
  <xs:simpleType>
    <xs:union>
      <xs:simpleType>
        <xs:restriction base='xs:string'>
          <xs:length value="0"/>
        </xs:restriction>
      </xs:simpleType>
      <xs:simpleType>
        <xs:restriction base='xs:integer' />
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>
</xs:element>

This gets your element the best of both types.

If you need more details: I found this solution on https://stackoverflow.com/questions/7109910/xsd-allow-element-type-as-integer-or-empty.

answered