<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="unqualified"
           attributeFormDefault="unqualified">


  <!-- SimpleType Declarations -->

  <xs:simpleType name="Complex">
    <xs:annotation>
      <xs:documentation>
        Two float with space as separator
      </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:pattern value="\d+\.\d*\s\d+\.\d*"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="None">
    <xs:annotation>
      <xs:documentation>
        Empty element
      </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:maxLength value="0"/>
    </xs:restriction>
  </xs:simpleType>

  <!-- ComplexType declarations -->

  <xs:complexType name="Reference">
    <xs:annotation>
      <xs:documentation>
        Its attribute id should refer to foreign key
      </xs:documentation>
    </xs:annotation>
    <xs:attribute name="id" type="xs:string"/>
  </xs:complexType>

  <xs:complexType name="Object">
    <xs:annotation>
      <xs:documentation>
      This element contains one tuple (__getinitargs__),
      and one dictionary (__dict__).
      Its attributes are id, module and class
      </xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="tuple" type="AllTypeList"/>
      <xs:element name="dictionary" type="Dictionary"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:string" use="required"/>
    <xs:attribute name="module" type="xs:string" use="required"/>
    <xs:attribute name="class" type="xs:string" use="required"/>
  </xs:complexType>

  <xs:complexType name="List">
    <xs:annotation>
      <xs:documentation>
      Extension of AllTypeList with id attribute
      </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="AllTypeList">
        <xs:attribute name="id" type="xs:string" use="required"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="Set">
    <xs:annotation>
      <xs:documentation>
      Extension of AllTypeList with id attribute
      </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="AllTypeList">
        <xs:attribute name="id" type="xs:string" use="required"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>


  <xs:complexType name="Dictionary">
    <xs:annotation>
      <xs:documentation>
      Extension of AllTypeList with id attribute
      Contains even occurences of AllTypeList
      XXX can not be asserted with XSD
      </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="AllTypeList">
        <xs:attribute name="id" type="xs:string" use="required"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:simpleType name="Boolean">
    <xs:annotation>
      <xs:documentation>
        0 or 1
      </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="1"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="AllTypeList">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="tuple" type="AllTypeList"/>
      <xs:element name="list" type="List"/>
      <xs:element name="set" type="Set"/>
      <xs:element name="dictionary" type="Dictionary"/>
      <xs:element name="int" type="xs:integer"/>
      <xs:element name="float" type="xs:float"/>
      <xs:element name="long" type="xs:double"/>
      <xs:element name="complex" type="Complex"/>
      <xs:element name="string" type="xs:string"/>
      <xs:element name="unicode" type="xs:string"/>
      <xs:element name="object" type="Object"/>
      <xs:element name="reference" type="Reference"/>
      <xs:element name="code" type="xs:string"/>
      <xs:element name="none" type="None"/>
      <xs:element name="bool" type="Boolean"/>
    </xs:choice>
  </xs:complexType>

  <!-- root Element -->

  <xs:element name="marshal" type="AllTypeList">
    <xs:annotation>
      <xs:documentation>
      Extension of AllTypeList.
      Assert that all id attributes are unique
      </xs:documentation>
    </xs:annotation>
    <xs:unique name="attribute_id">
      <xs:selector xpath=".//object|.//list|.//dictionary"/>
      <xs:field xpath="@id"/>
    </xs:unique>
    <xs:key name="id_list">
      <xs:selector xpath=".//object|.//list|.//dictionary"/>
      <xs:field xpath="@id"/>
    </xs:key>
    <xs:keyref  name="id_list_reference" refer="id_list">
      <xs:selector xpath=".//reference"/>
      <xs:field xpath="@id"/>
    </xs:keyref> 
  </xs:element>

</xs:schema>


