<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  targetNamespace="http://www.nexml.org/1.0"
  xmlns="http://www.nexml.org/1.0">
  <xs:annotation>
    <xs:documentation>
      This module constitutes in effect the 'main' class of the nexml schema.
      It defines the complex type for the root &lt;<a href="#Nexml">nex:nexml</a>&gt; 
      element and a simple type for the schema <a href="#Nexml1_0">"version" attribute</a> 
      of the root element. In addition, this module includes the main schema modules for 
      <a href="../taxa/taxa">OTUs</a>, <a href="../characters/characters">characters</a>, 
      <a href="../trees/trees">trees</a> and <a href="../meta/meta">metadata</a>. The data 
      type definitions in these included schema modules are then used to define element 
      instances in the child substructures of the &lt;nex:nexml&gt; root element.
      
      $Id: $
    </xs:documentation>
  </xs:annotation>
  
  <xs:include schemaLocation="taxa/taxa.xsd"/>
  <xs:include schemaLocation="characters/characters.xsd"/>
  <xs:include schemaLocation="trees/trees.xsd"/>
  <xs:include schemaLocation="meta/meta.xsd"/>
  <!--<xs:include schemaLocation="models/models.xsd"/>-->
  
  <xs:simpleType name="Nexml1_0">
    <xs:annotation>
      <xs:documentation>
        This simple type is the fixed version value, which in this
        iteration must be constrained to 1.0 (and will be incremented
        in future versions).
      </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:decimal">
      <xs:pattern value="1\.0"/>
    </xs:restriction>
  </xs:simpleType>
  
  <xs:complexType name="Nexml">
    <xs:annotation>
      <xs:documentation>
        The root element for nexml.
      </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="Annotated">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
          <xs:element name="otus" type="Taxa" minOccurs="1" maxOccurs="unbounded"/>        
          <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:choice>
              <xs:element name="characters" type="AbstractBlock" minOccurs="0" maxOccurs="unbounded"/>                                                                 
              <xs:element name="trees" type="Trees" minOccurs="0" maxOccurs="unbounded">
                <xs:key name="tree">
                  <xs:selector xpath="./tree"/>
                  <xs:field xpath="@id"/>
                </xs:key>
              </xs:element>
            </xs:choice>
          </xs:sequence>     
        </xs:sequence>
        <xs:attribute name="version" type="Nexml1_0" use="required"/>
        <xs:attribute name="generator" type="xs:string" use="optional"/>        
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <!-- the root of the document -->
  <xs:element name="nexml" type="Nexml">

    <!-- keys for child-of-root biodata containers: taxa, characters trees -->    
    <xs:key name="taxa">
      <xs:selector xpath="./taxa"/>
      <xs:field xpath="@id"/>
    </xs:key>
    <xs:key name="characters">
      <xs:selector xpath="./characters"/>
      <xs:field xpath="@id"/>
    </xs:key>
    <xs:key name="trees">
      <xs:selector xpath="./trees"/>
      <xs:field xpath="@id"/>
    </xs:key>
    
    <xs:key name="taxon">
      <xs:selector xpath="./taxa/taxon"/>
      <xs:field xpath="@id"/>
    </xs:key>    

    <!-- links to taxon objects: from row to taxon, from node to taxon -->
    <xs:keyref name="rowTaxonRef" refer="taxon">
      <xs:selector xpath="./characters/matrix/row"/>
      <xs:field xpath="@taxon"/>
    </xs:keyref>
    <xs:keyref name="listNodeTaxonRef" refer="taxon">
      <xs:selector xpath="./trees/tree/terminal"/>
      <xs:field xpath="@taxon"/>
    </xs:keyref>
    <xs:keyref name="nestedNodeTaxonRef" refer="taxon">
      <xs:selector xpath="./trees/tree/root/internal/*/terminal"/>
      <xs:field xpath="@taxon"/>
    </xs:keyref>    

  </xs:element>
</xs:schema>
