<?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="SheetAttrs.xsd"/>
  <xs:include schemaLocation="SheetEvents.xsd"/>
  <xs:include schemaLocation="GridSheet.xsd"/>
  <xs:include schemaLocation="FieldsSheet.xsd"/>
  
  <xs:group name="DocumentSheets_RulesGroup">
    <xs:sequence>
      <xs:element name="rules" minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:documentation xml:lang="ru-ru">
            События документа.
          </xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <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="DocumentSheets_Events"/>
                      </xs:simpleType>
                    </xs:attribute>
                  </xs:restriction>
                </xs:complexContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
        <xs:unique name="DocumentSheets_Rules_UniqueRules">
          <xs:selector xpath="rule"/>
          <xs:field xpath="@event"/>
        </xs:unique>
      </xs:element>
    </xs:sequence>
  </xs:group>

  <xs:complexType name="GroupSheetType">
    <xs:annotation>
      <xs:documentation xml:lang="ru-ru">
        Группирующий лист. Содержит в себе другие листы. Должен быть описан раньше листов которые он содержит.
      </xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:group ref="Sheets_Sequence"/>
      <xs:element name="rules" minOccurs="0" maxOccurs="1">
        <xs:annotation>
          <xs:documentation xml:lang="ru-ru">
            События группирующего листа.
          </xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <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="Sheet_Events"/>
                      </xs:simpleType>
                    </xs:attribute>
                  </xs:restriction>
                </xs:complexContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
        <xs:unique name="GroupSheet_Rules_UniqueRules">
          <xs:selector xpath="rule"/>
          <xs:field xpath="@event"/>
        </xs:unique>
      </xs:element>
    </xs:sequence>    
    <xs:attributeGroup ref="Sheet_Attrs"/>
    <xs:attributeGroup ref="NonTemplateSheet_Attrs"/>
    <xs:attributeGroup ref="GridAndFieldsSheet_Attrs" />
  </xs:complexType>

  <xs:complexType name="GridSheetType">
    <xs:annotation>
      <xs:documentation xml:lang="ru-ru">
        Табличный лист. Содержит таблицу состоящую из граф. Строится на основании таблицы источника генерируемой на основании его описания. 
      </xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:group ref="GridSheetContentSequence" minOccurs="1" maxOccurs="unbounded"/>
      <xs:element name="menuitems" minOccurs="0" maxOccurs="1" type="MainMenuItemsType"/>
      <xs:element name="rules" minOccurs="0" maxOccurs="1">
        <xs:annotation>
          <xs:documentation xml:lang="ru-ru">
            События табличного листа.
          </xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <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="Sheet_Events GridAndFieldsSheet_Events GridSheet_Events"/>
                      </xs:simpleType>
                    </xs:attribute>
                  </xs:restriction>
                </xs:complexContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
        <xs:unique name="GridSheet_Rules_UniqueRules">
          <xs:selector xpath="rule"/>
          <xs:field xpath="@event"/>
        </xs:unique>
      </xs:element>
    </xs:sequence>
    <xs:attributeGroup ref="Sheet_Attrs"/>
    <xs:attributeGroup ref="NonTemplateSheet_Attrs"/>
    <xs:attributeGroup ref="GridAndFieldsSheet_Attrs"/>
    <xs:attributeGroup ref="GridSheet_Attrs"/>
  </xs:complexType>

  <xs:complexType name="TemplateSheetType">
    <xs:annotation>
      <xs:documentation xml:lang="ru-ru">
        Шаблон табличного листа. Генерируется на основании другого табличного листа описанного ранее. 
      </xs:documentation>
    </xs:annotation>
    <xs:attributeGroup ref="Sheet_Attrs"/>
    <xs:attributeGroup ref="TemplateSheet_Attrs"/>
  </xs:complexType>

  <xs:complexType name="FieldsSheetType">
    <xs:annotation>
      <xs:documentation xml:lang="ru-ru">
        Лист с полями ввода. Дает возможность редактировать одну запись определенной таблицы, согласно описанной в нем логике.
      </xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:group ref="FieldsSheet_FieldsSequence" minOccurs="1" maxOccurs="unbounded"/>
      <xs:element name="rules" minOccurs="0" maxOccurs="1">
        <xs:annotation>
          <xs:documentation xml:lang="ru-ru">
            События листа с полями ввода.
          </xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <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="Sheet_Events GridAndFieldsSheet_Events FieldsSheet_Events"/>
                      </xs:simpleType>
                    </xs:attribute>
                  </xs:restriction>
                </xs:complexContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
        <xs:unique name="FieldSheet_Rules_UniqueRules">
          <xs:selector xpath="rule"/>
          <xs:field xpath="@event"/>
        </xs:unique>
      </xs:element>
    </xs:sequence>
    <xs:attributeGroup ref="Sheet_Attrs"/>
    <xs:attributeGroup ref="NonTemplateSheet_Attrs"/>    
    <xs:attributeGroup ref="GridAndFieldsSheet_Attrs"/>
    <xs:attributeGroup ref="FieldsSheet_Attrs"/>
  </xs:complexType>

  <xs:group name="Sheets_Sequence">
    <xs:sequence>
      <xs:element name="groupsheet" minOccurs="0" maxOccurs="unbounded" type="GroupSheetType"/>
      <xs:element name="gridsheet" minOccurs="0" maxOccurs="unbounded" type="GridSheetType"/>
      <xs:element name="generatedsheet" minOccurs="0" maxOccurs="unbounded" type="TemplateSheetType"/>
      <xs:element name="fieldssheet" minOccurs="0" maxOccurs="unbounded" type="FieldsSheetType">
        <xs:unique name="FieldSheet_UniqueFieldNames">
          <xs:selector xpath="field"/>
          <xs:field xpath="@name"/>
        </xs:unique>
      </xs:element>
    </xs:sequence>
  </xs:group>

</xs:schema>
