<?xml version="1.0" encoding="utf-8" ?> 
<xs:schema targetNamespace="http://www.adammil.net/math/math.xsd" elementFormDefault="unqualified" 
           xmlns="http://www.adammil.net/math/math.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!-- an empty element -->
  <xs:complexType name="empty" />
  
  <!-- a reference -->
  <xs:complexType name="ref">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="ref" type="xs:string" />
        <!-- the id of another item, or the refText of a subdef -->
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <!-- an unordered list (ul) -->
  <xs:complexType name="ul">
    <xs:sequence>
      <xs:element name="li" type="text" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>

  <!-- an ordered list (ol) -->
  <xs:complexType name="ol">
    <xs:complexContent>
      <xs:extension base="ul">
        <xs:attribute name="type">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="1" />
              <xs:enumeration value="a" />
              <xs:enumeration value="A" />
              <xs:enumeration value="i" />
              <xs:enumeration value="I" />
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  
  <!-- a table (table) -->
  <xs:complexType name="table">
    <xs:sequence>
      <xs:element name="tr" type="tr" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <!-- a table row (tr) -->
  <xs:complexType name="tr">
    <xs:choice maxOccurs="unbounded">
      <xs:element name="td" type="text" /> <!-- a table data cell -->
      <xs:element name="th" type="text" /> <!-- a table header cell -->
    </xs:choice>
  </xs:complexType>

  <!-- a type for mixed text content (text) -->
  <xs:complexType name="text" mixed="true">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <!-- a subdefinition, like x = 2y     (i), where "i" is the refText, typically right-aligned -->
      <xs:element name="subdef">
        <xs:complexType>
          <xs:complexContent>
            <xs:extension base="text">
              <!-- if the refText is omitted, the subdef is rendered by itself, usually indented -->
              <xs:attribute name="refText" type="xs:string" />
            </xs:extension>
          </xs:complexContent>
        </xs:complexType>
      </xs:element>
      
      <xs:element name="imp" type="xs:string" /> <!-- important text (typically bolded) -->
      <xs:element name="emph" type="xs:string" /> <!-- emphasized text (typically italicized) -->
      <xs:element name="el" type="empty" /> <!-- inserts an empty line -->
      <xs:element name="ul" type="ul" /> <!-- an unordered list -->
      <xs:element name="ol" type="ol" /> <!-- an ordered list -->
      <xs:element name="table" type="table" />

      <!-- a snippet of math, using ASCIIMathML syntax. as a shortcut, a snippet of math can be enclosed in backticks
        rather than an <m> element
      -->
      <xs:element name="m" type="xs:string" />

      <!-- a reference to another item (subdef, definition, theorem, etc). the text content is the text content of the
        link. if the text content is omitted, it will be automatically generated.
      -->
      <xs:element name="r" type="ref" />

      <!-- a reference to the definition of an item. this is identical to <r> except that the default link text will
        say "the definition of X" rather than just "X".
      -->
      <xs:element name="d" type="ref" />
    </xs:choice>
  </xs:complexType>

  <!-- attributes common to definitions, theorems, hypotheses, etc. -->
  <xs:attributeGroup name="defAttrs">
    <xs:attribute name="id" type="xs:ID" use="required" />
    <xs:attribute name="name" type="xs:string" use="required" />
    <xs:attribute name="shortMath" type="xs:string" />
    <xs:attribute name="tags">
      <xs:simpleType>
        <xs:list itemType="xs:string" />
      </xs:simpleType>
    </xs:attribute>
  </xs:attributeGroup>

  <!-- base type for definitions, hypotheses, etc. -->
  <xs:complexType abstract="true" name="defBase" mixed="true">
    <xs:complexContent>
      <xs:extension base="text">
        <xs:attributeGroup ref="defAttrs" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <!-- the base for hypotheses, including theorems and corollaries -->
  <xs:complexType name="hypothesisBase" mixed="true">
    <xs:complexContent>
      <xs:extension base="defBase">
        <xs:attribute name="shortName" type="xs:string" />
        <!-- the text that will be shown in references to this -->
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <!-- a hypothesis. that is, an unproved theorem -->
  <xs:complexType name="hypothesis" mixed="true">
    <xs:complexContent>
      <xs:extension base="hypothesisBase">
        <xs:sequence>
          <xs:element name="text" type="text" minOccurs="0" />
          <!-- text to justify the hypothesis -->
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <!-- a type for definitions (definition). the definition's name N is typically rendered "definition of N" -->
  <xs:complexType name="definition" mixed="true">
    <xs:complexContent>
      <xs:extension base="defBase">
        <xs:sequence>
          <xs:element name="text" type="text" minOccurs="0" />
        </xs:sequence>
        <!-- a list of tags that are defined here. if omitted, the definition's name is used as the tag value -->
        <xs:attribute name="defines">
          <xs:simpleType>
            <xs:list itemType="xs:string" />
          </xs:simpleType>
        </xs:attribute>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <!-- a type for hypothetical corollaries (corollary) -->
  <xs:complexType name="hypoCorollary">
    <xs:complexContent>
      <xs:extension base="hypothesis">
        <!-- the hypothesis of which this is a corollary -->
        <xs:attribute name="ref" type="xs:IDREF" use="required" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <!-- base type for theorems and corollaries (theoremBase) -->
  <xs:complexType name="theorem">
    <xs:sequence>
      <xs:element name="text" type="text" />
      <xs:element name="proof" type="text" />
    </xs:sequence>
    <xs:attributeGroup ref="defAttrs" />
    <xs:attribute name="shortName" type="xs:string" />
  </xs:complexType>

  <!-- a type for corollaries (corollary) -->
  <xs:complexType name="corollary">
    <xs:complexContent>
      <xs:extension base="theorem">
        <!-- the theorem of which this is a corollary -->
        <xs:attribute name="ref" type="xs:IDREF" use="required" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <!-- a math topic (topic) -->
  <xs:complexType name="topic">
    <xs:sequence>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="definition" type="definition" />
        <xs:element name="hypothesis" type="hypothesis" />
        <xs:element name="hypoCorollary" type="hypoCorollary" />
        <xs:element name="theorem" type="theorem" />
        <xs:element name="corollary" type="corollary" />
        <xs:element name="box" type="text" />
        <xs:element name="text" type="text" />
      </xs:choice>
      <xs:element name="topic" type="topic" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>

    <xs:attribute name="id" type="xs:ID" use="required" />
    <xs:attribute name="name" type="xs:string" use="required" />
    <xs:attribute name="description" type="xs:string" use="required" />
  </xs:complexType>
  
  <!-- root element (math) -->
  <xs:element name="math">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="topic" type="topic" minOccurs="0" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>  
  </xs:element>
  
</xs:schema>
