<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include schemaLocation="Base.xsd"/>
  <xs:include schemaLocation="TableFields.xsd"/>
  <xs:include schemaLocation="TableAttrs.xsd"/>
  <xs:include schemaLocation="TableEvents.xsd"/>  

  <xs:group name="Tables_Sequence">
    <xs:sequence>
      <xs:element name="linkedtable" minOccurs="0" maxOccurs="unbounded" type="LinkedTableType"/>
      <xs:element name="extendedtable" minOccurs="0" maxOccurs="unbounded" type="ExtendedTableType"/>
      <xs:element name="table" minOccurs="0" maxOccurs="unbounded" type="DataTableType"/>
    </xs:sequence>
  </xs:group>

  <xs:complexType name="DataTableType">
    <xs:annotation>
      <xs:documentation xml:lang="ru-ru">
        Таблица с данными.
      </xs:documentation>
    </xs:annotation>
        <xs:sequence>
          <xs:element name="fields" minOccurs="1" maxOccurs="1" type="DataTable_FieldsType">
            <xs:annotation>
              <xs:documentation>
                Поля таблицы.
              </xs:documentation>
            </xs:annotation>
          </xs:element>
          <xs:element name="rules" minOccurs="0" maxOccurs="1" type="DataTable_RulesType">
            <xs:annotation>
              <xs:documentation xml:lang="ru-ru">
                События таблицы с данными.
              </xs:documentation>
            </xs:annotation>
            <xs:unique name="DataTable_Rules_UniqueRules">
              <xs:selector xpath="rule"/>
              <xs:field xpath="@event"/>
            </xs:unique>
          </xs:element>
          <xs:element name="data" minOccurs="0" maxOccurs="1" type="DataTable_DataType">
            <xs:annotation>
              <xs:documentation>
                Данные таблицы.
              </xs:documentation>
            </xs:annotation>
          </xs:element>
        </xs:sequence>
        <xs:attributeGroup ref="NotLinkedTable_Attrs"/>
        <xs:attributeGroup ref="DataTable_Attrs"/>
  </xs:complexType>

  <xs:complexType name="DataTable_RulesType">
    <xs:sequence>
      <xs:element name="rule" minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:documentation xml:lang="ru-ru">
            Событие таблицы с данными.
          </xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:complexContent>
            <xs:restriction base="EventItemType">
              <xs:attribute name="event" use="required">
                <xs:simpleType>
                  <xs:union memberTypes="Table_Events DataTable_Events"/>
                </xs:simpleType>
              </xs:attribute>
            </xs:restriction>
          </xs:complexContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="DataTable_FieldsType">
    <xs:sequence>
      <xs:group ref="DataTable_FieldsGroup" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:group name="DataTable_FieldsGroup">
    <xs:choice>
      <xs:element name="field" minOccurs="0" maxOccurs="unbounded" type="DataTable_DataFieldType"/>
      <xs:element name="sourcefield" minOccurs="0" maxOccurs="unbounded" type="DataTable_SourceFieldType"/>
      <xs:element name="lookupfield" minOccurs="0" maxOccurs="unbounded" type="DataTable_LookupFieldType"/>
      <xs:element name="calcfield" minOccurs="0" maxOccurs="unbounded" type="DataTable_CalcFieldType"/>
    </xs:choice>
  </xs:group>

  <xs:complexType name="DataTable_DataType">
    <xs:sequence>
      <xs:element name="row" minOccurs="0" maxOccurs="unbounded" type="DataTable_Data_RowType"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="DataTable_Data_RowType">
    <xs:annotation>
      <xs:documentation xml:lang="ru-ru">
        Строка данных.
      </xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="el" minOccurs="0" maxOccurs="unbounded" type="xs:string">
        <xs:annotation>
          <xs:documentation xml:lang="ru-ru">
            Указывает данные в поле данных.
          </xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:sequence>
    <xs:attributeGroup ref="DataTable_Data_RowType_AttrGroup" />
  </xs:complexType>

  <xs:complexType name="LinkedTableType">
    <xs:annotation>
      <xs:documentation>
        Таблица из другого файла.
      </xs:documentation>
    </xs:annotation>
    <xs:attributeGroup ref="LinkedTable_Attrs"/>
  </xs:complexType>

  <xs:complexType name="ExtendedTableType">
    <xs:annotation>
      <xs:documentation>
        Таблица строящаяся на основе данных другой таблицы. Расширенная таблица.
      </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="ExtendedTable_RulesType">
        <xs:sequence>
          <xs:element name="fields" minOccurs="0" maxOccurs="1" type="ExtendedTable_FieldsType" />
          <xs:element name="rules"  minOccurs="0" maxOccurs="1" type="ExtendedTable_RulesType"/>
        </xs:sequence>
        <xs:attributeGroup ref="NotLinkedTable_Attrs"/>
        <xs:attributeGroup ref="ExtendedTable_Attrs"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="ExtendedTable_RulesType">
    <xs:annotation>
      <xs:documentation xml:lang="ru-ru">
        События расширенной таблицы.
      </xs:documentation>
    </xs:annotation>
          <xs:sequence>
            <xs:element name="rule" minOccurs="0" maxOccurs="unbounded">
              <xs:annotation>
                <xs:documentation xml:lang="ru-ru">
                  Событие расширенной таблицы.
                </xs:documentation>
              </xs:annotation>
              <xs:complexType>
                <xs:complexContent>
                  <xs:restriction base="EventItemType">
                    <xs:attribute name="event" use="required">
                      <xs:simpleType>
                        <xs:union memberTypes="Table_Events ExtendedTable_Events DataTable_Events"/>
                      </xs:simpleType>
                    </xs:attribute>
                  </xs:restriction>
                </xs:complexContent>
              </xs:complexType>
              <xs:unique name="ExtendedTableType_Rules_UniqueRules">
                <xs:selector xpath="rule"/>
                <xs:field xpath="@event"/>
              </xs:unique>
            </xs:element>
          </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ExtendedTable_FieldsType">
    <xs:annotation>
      <xs:documentation xml:lang="ru-ru">
        Поля расширенной таблицы.
      </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="ExtendedTable_Field_RulesType">
        <xs:sequence>
          <xs:element name="field" minOccurs="0" maxOccurs="unbounded" >
            <xs:complexType>
              <xs:complexContent>
                <xs:extension base="ExtendedTable_Field_RulesType">
                  <xs:attributeGroup ref="ExtendedField_Attrs"/>
                </xs:extension>
              </xs:complexContent>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="ExtendedTable_Field_RulesType">
    <xs:sequence>
      <xs:element name="rules" minOccurs="0" maxOccurs="1" >
        <xs:complexType>
          <xs:annotation>
            <xs:documentation xml:lang="ru-ru">
              События поля расширенной таблицы.
            </xs:documentation>
          </xs:annotation>
          <xs:sequence>
            <xs:element name="rule" minOccurs="0" maxOccurs="unbounded" >
              <xs:annotation>
                <xs:documentation xml:lang="ru-ru">
                  Событие поля расширенной таблицы.
                </xs:documentation>
              </xs:annotation>
              <xs:complexType>
                <xs:complexContent>
                  <xs:restriction base="EventItemType">
                    <xs:attribute name="event" use="required">
                      <xs:simpleType>
                        <xs:union memberTypes="ExtendedTable_Field_Events"/>
                      </xs:simpleType>
                    </xs:attribute>
                  </xs:restriction>
                </xs:complexContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
        <xs:unique name="ExtendedTable_Fields_RulesType_UniqueRules">
          <xs:selector xpath="rule"/>
          <xs:field xpath="@event"/>
        </xs:unique>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

</xs:schema>