XML Schema Support

6 minutes to read Download PDF Edit

Mendix derives the input/output formats for XML import/export and for calling SOAP/XML web services by interpreting XML Schema Definition (XSD) files. When you import an XML schema (.xsd file) or web service definition (.wsdl file) using the Mendix Business Modeler, you might get a dialog that contains warning messages about unsupported constructs. This is because currently Mendix does not support XSD in its entirety. The mapping in Mendix is based on entities and attributes, and some XSD constructs do not lend themselves easily for this format.

There are two kinds of XML mappings in Mendix: XML-to-domain mapping, which translates XML data to Mendix objects, and domain-to-XML mapping, which does the opposite. XML-to-domain mappings are used when importing XML files using the ‘Import XML’ activity in microflows, and for processing the response of a web service call. Domain-to-XML mappings are used for exporting XML files in microflows and for generating the XML for the request of a web service call.

XML-to-domain mappings support almost all XSD constructs. In the mapping editor, you can check elements that can occur in the XML according to the XSD and then define a mapping to Mendix objects for those elements. It does not matter much how often or in which order elements occur; whenever an element occurs, the corresponding mapping for that element is applied.

Domain-to-XML mappings are a bit more complicated. The XML that is generated using such a mapping must strictly adhere to the XSD specification. This means that the XML tags that are generated by the mapping must be in the exact order that the XSD specifies. As the mapping is based on the Mendix domain model (i.e., objects), currently only a subset of all possible XSD constructs is supported.

Elements and Types

XSD defines elements that correspond to tags in XML. Elements have types that define their contents, e.g., a primitive value or a list of child elements. Elements can have either a simple type or a complex type, while a complex type can have either simple or complex content. For both simple types and complex types with simple content, the content of the element is a primitive value, such as an integer or string value. For complex types with complex content, the content of the element can consist of several child elements. Complex types can also define attributes that can occur within the XML tag.

The following example shows an XML schema and an XML instance that adheres to the schema. The schema defines a ‘customer’ element with a complex type with complex content, namely a sequence of the elements ‘name’ and ‘shoesize’. The ‘name’ element has a simple type, namely ‘string’. The ‘shoesize’ element has a complex type with simple content; it extends the simple type ‘integer’ by adding a ‘country’ attribute.

Example XML schema

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.example.com/" elementFormDefault="qualified" xmlns:tns="http://www.example.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="customer">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="shoesize" type="tns:shoesizeType"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="shoesizeType">
    <xs:simpleContent>
      <xs:extension base="xs:integer">
        <xs:attribute name="country" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>

Example XML instance

<?xml version="1.0" encoding="utf-8"?>
<customer xmlns="http://www.example.com/">
  <name>John Doe</name>
  <shoesize country="GB">8</shoesize>
</customer>

As XML mappings in Mendix are based on the domain model, which consists of entities, attributes and associations, the XSD elements are translated to a sort of object model when you create a mapping. Elements that have complex types or occur more than once are translated to so-called ‘object elements’ that are mapped to an entity. Elements that have a simple type and occur at most once are translated to ‘value elements’ that are mapped to an attribute of the entity of their containing object element. XML attributes of complex types are also translated to value elements.

The following image shows how the XML schema of the previous example is translated to a mapping in Mendix. It is an XML-to-domain mapping for the ‘customer’ element of the example. The ‘customer’ element is translated to an object element (gray rectangle), while the ‘name’ and ‘shoesize’ elements and the ‘country’ attribute of the ‘shoesize’ element are translated to value elements in the mapping.

Attributes

Complex types can specify attributes that can occur within the XML tag. These attributes always have a simple type and can occur at most once. Attributes are fully supported in both mapping types. Attributes are translated to value elements in the mapping and can therefore be mapped to an attribute of an entity.

Simple Types and Complex Types with Simple Content

Mendix fully supports elements with primitive content, i.e., simple types or complex types with simple content. However, Mendix currently does not take restrictions of simple types into account, such as limiting a string to a finite set of possibilities or limiting the range of an integer. In those cases, all possible values of the base type (e.g., string) are allowed.

Usually, elements with primitive content are translated to value elements in the mapping. If for some reason an element with primitive content is translated to an object element in the mapping, for instance because it occurs more than once, or because it has a complex type that also defines attributes, the contents of the element are translated to an extra value element called ‘(Contents)’.

Complex Types with Complex Content

For elements having a complex type with complex content, the content of the element can consist of several child elements of which the order and multiplicity is defined by one or more grouping constructs such as choice and sequence. XML-to-domain mappings fully support these grouping constructs, as it does not matter much in which order and multiplicity the elements occur in the XML. Every time an element is encountered, its corresponding mapping is executed.

Some of these grouping constructs are not fully supported in domain-to-XML mappings, however, because they do not translate naturally to entities and attributes. For example, the choice construct is currently not supported and sequences are only supported if they occur at most once. The following table shows which grouping constructs are supported in domain-to-XML mappings.

Construct Supported in domain-to-XML mappings
group Yes
sequence Only if it occurs exactly once
choice Only if each child element of the choice occurs at most once. Before version 3.2 choice was not supported.
all No

Mixed Content

Complex types can define their content to be mixed. This means that an element has both text and child elements as contents. Mixed content is commonly found in document data such as (X)HTML, and less commonly in structured data. Currently, mixed content is not supported in Mendix.