×
Menu
Index

Using schema's

Introduction

This chapter describes a bit of theory about schemas. To learn more about XML schemas, please visit http://www.w3schools.com/schema.
 

Schemas

A schema describes the structure of an XML document.
 
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="CL">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="CL_VRWRKCD" type="xs:anyType"/>
        <xs:element name="CL_VOLGNUM" type="xs:anyType"/>
        <xs:element name="CLAUSNR" type="xs:anyType"/>
        <xs:element name="CLAUSOM" type="xs:anyType"/>
        <xs:element name="CLT001" type="xs:anyType"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
 
An XML schema defines:
 

<schema> Element

The  <schema> element is the root element of any XML schema.
<?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>
 
A <schema> element can contain attributes. Example:
 
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.comparity.nl" xmlns="http://www.comparity.nl" elementFormDefault="qualified">
...
...
</xs:schema>
 
The fragment: xmlns:xs="http://www.w3.org/2001/XMLSchema" describes that the elementen and data types in the schema come from the “http://www.w3.org/2001/XMLSchema” namespace. This also implicates that elements coming from this namespace should be prefixed with xs:
 
This fragment: targetNamespace="http://www.comparity.nl" implicates that elements defined in this schema (like CL, CL_VOLGNUM, etc.) come from the “http://www.comparity.nl” namespace.
 
The fragment: xmlns="http://www.comparity.nl" describes that the standard namespace is “http://www.comparity.nl”.
 
The fragment: elementFormDefault="qualified" describes that all elements in an XML instance from this schema should follow this namespace.
 

Simple Element

A simple element is an XML element that can contain only text. A simple element can't contain other elements or attributes. Despite the fact that it can contain only text, this text can be of several types (boolean, string, date, etc.). You can also define your own types.
 
To declare a simple element, use the following syntax:
 
<xs:element name="xxx" type="yyy"/>
 
where xxx is the name of the element and yyy the type.
 
Mostly used types:
Example:
<xs:element name="CL_VOLGNUM" type="xs:integer"/>
 

Attributes

Simple elements can't have attributes. If an element contains attributes, it will be seen as a complex type. But the attribute itself will be declared as a simple type.
 
To declare an attribute, use the following syntax:
 
<xs:attribute name="xxx" type="yyy"/>
 
where xxx is the name of the attribute and yyy the type.
 
Example:
 
<xs:attribute name="NR" type="xs:integer"/>
 
This will describe the following XML element:
 
<L NR=1234>waarde</L>
 

Complex Element

Een complex element is een XML element dat andere elementen of attributen bevat. Een complex element wordt als volgt gedeclareerd:
 
<xs:element name="CL">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="CL_VOLGNUM" type="xs:integer"/>
      <xs:element name="CL_VRWRKCD" type="xs:integer"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
 

Indicators

There are seven indicators:
 

Order indicators

 

Occurance indicators

Group indicators

Order indicators

Order indicators are used to define the order of the elements.
 

All indicator

Using the All indicator you specify that child elements can occur in any order and that they may only occur ones.
<xs:element name="person">
  <xs:complexType>
    <xs:all>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:all>
  </xs:complexType>
</xs:element>
 

Choice indicator

Using the choice indicator you specify that either one of the elements may exist, but never more than one.
<xs:element name="person">
  <xs:complexType>
    <xs:choice>
      <xs:element name="employee" type="employee"/>
      <xs:element name="member" type="member"/>
    </xs:choice>
  </xs:complexType>
</xs:element>
 

Sequence indicator

Using the sequence indicator you specify that the child elements should follow the described order.
<xs:element name="person">
   <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
 

Occurance indicators

Using the occurance indicator you specify how many times an element may occur.
<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="fullname" type="xs:string"/>
      <xs:element name="childname" type="xs:string" maxOccurs="10"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
 

Group indicators

For more information about group indicators, see:
 

Include

Using Include you can load another XSD file from another location.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include schemaLocation="ENTITEITEN.xsd"/>
  <xs:element name="Relatiedocument">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="SoftRules" />
        <xs:element ref="Relatiemantel" />
        <xs:element ref="Contract" />
        <xs:element ref="Pakket" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
 
The file (ENTITEITEN.xsd) may look like this:
 
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include schemaLocation="Contract.xsd"/>
  <xs:include schemaLocation="Relatiemantel.xsd"/>
  <xs:include schemaLocation="Pakket.xsd"/>
  <xs:include schemaLocation="AA.xsd"/>
  <xs:include schemaLocation="AB.xsd"/>
  <xs:include schemaLocation="AC.xsd"/>
 
  ...
 
  <xs:include schemaLocation="ZW.xsd"/>
</xs:schema>
 
AA.xsd can look like this:
 
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="AA">
    <xs:sequence>
      <xs:element name="AA_ENTITEI">
        <xs:annotation>
          <xs:appinfo>
            <Description>Entiteitscode</Description>
            <TextValuesID>ADNENT</TextValuesID>
          </xs:appinfo>
        </xs:annotation>
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:maxLength value="2" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
 
      ...
 
      <xs:element name="AA_FINLEAS">
        <xs:annotation>
          <xs:appinfo>
            <Description>Lease aand., code</Description>
            <TextValuesID>ADNLEA</TextValuesID>
          </xs:appinfo>
        </xs:annotation>
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:length value="1" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
 

ComplexContent

Using ComplexContent you can use Complex Elements, defined elsewhere and eventually expand them.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Contract">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="AL">
          <xs:complexType>
            <xs:complexContent mixed="false">
              <xs:extension base="AL">
                <xs:sequence>
                  <xs:element name="BY">
                    <xs:complexType>
                      <xs:complexContent mixed="false">
                        <xs:extension base="BY">
                          <xs:sequence />
                        </xs:extension>
                      </xs:complexContent>
                    </xs:complexType>
                  </xs:element>
                  <xs:element name="MY">
                    <xs:complexType>
                      <xs:complexContent mixed="false">
                        <xs:extension base="MY">
                          <xs:sequence />
                        </xs:extension>
                      </xs:complexContent>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:extension>
            </xs:complexContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
 
Attention!
The use of namespaces in the XML input document is not fully supported. Please contact us about the limitations and send us an email support@softrules.com. Maybe we can search for a solution or give you some tips.