<?xml version="1.0"?>
<xs:schema targetNameSpace="http://amisgourmands.fr/schemas/0.1/recipe"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="recipe" type="recipe">
  <xs:annotation>
    <xs:documentation xml:lang="en">
     Recipe schema
     Version 0.2
     Copyright 2008 AmisGourmands.fr.
    </xs:documentation>
  </xs:annotation>
</xs:element>

<xs:complexType name="recipe">
  <xs:sequence>
    <xs:element name="title" type="xs:string" minOccurs="1" maxOccurs="1"/>
    <xs:element name="catalogued_in" type="catalogued_in"
                 minOccurs="0" maxOccurs="1"/>
    <xs:element name="ingredient_lines" type="ingredient_lines"
                 minOccurs="1" maxOccurs="1"/>
    <xs:choice>
      <xs:element name="instructions" type="xs:string"
                  minOccurs="0" maxOccurs="1"/>
      <xs:element name="steps" type="steps"
                  minOccurs="0" maxOccurs="1"/>
    </xs:choice>
    <xs:element name="notes" type="xs:string"
                 minOccurs="0" maxOccurs="1"/>
    <xs:element name="change_history" type="change_history"
                minOccurs="0" maxOccurs="1"/>
  </xs:sequence>
  <xs:attribute name="owned_by" type="xs:string" use="optional"/>
  <xs:attribute name="measures" type="xs:string" use="optional"/>
  <xs:attribute name="schema_version" type="xs:string" use="optional"/>
</xs:complexType>

<xs:complexType name="catalogued_in">
  <xs:sequence>
    <xs:element name="catalog" type="catalog"
                minOccurs="1" maxOccurs="unbounded"/>
  </xs:sequence>
</xs:complexType> 

<xs:complexType name="catalog">
 <xs:simpleContent>
   <xs:extension base="xs:string">
     <xs:attribute name="name" type="xs:string" use="required"/>
   </xs:extension>
 </xs:simpleContent>
</xs:complexType>

<xs:complexType name="ingredient_lines">
  <xs:sequence>
    <xs:element name="yield" type="quantity" minOccurs="1" maxOccurs="1"/>
    <xs:element name="ingredient_line" type="ingredient_line"
                minOccurs="1" maxOccurs="unbounded"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="ingredient_line">
  <xs:sequence>
    <xs:choice>
      <xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="1"/>
      <xs:element name="quantity" type="quantity"
                  minOccurs="0" maxOccurs="1"/>
    </xs:choice>
    <xs:choice>
      <xs:element name="ingredient" type="xs:string"/>
      <xs:element name="ingredient_line" type="ingredient_line"
                  minOccurs="1" maxOccurs="unbounded"/>
      <xs:element name="recipe" type="xs:string"/> 
    </xs:choice>
  </xs:sequence>
  <xs:attribute name="type" type="ingredient_line_type"
                use="optional" default="simple"/>
</xs:complexType>

<xs:simpleType name="ingredient_line_type">
  <xs:annotation>
    <xs:documentation xml:lang="en">
     Following types of ingredient lines are allowed:
     - simple: simple ingredient
     - group: group of ingredients
     - ref: reference to another recipe 
    </xs:documentation>
  </xs:annotation>
  <xs:restriction base="xs:string">
    <xs:enumeration value="simple"/>
    <xs:enumeration value="group"/>
    <xs:enumeration value="ref"/>
  </xs:restriction>
</xs:simpleType>

<xs:complexType name="quantity">
  <xs:annotation>
    <xs:documentation xml:lang="en">
    A quantity must have a dimension (either a unit or a piece of something)
    and can be qualified by a qualifier (e.g. big, small). 
    </xs:documentation>
  </xs:annotation>
  <xs:simpleContent>
    <xs:extension base="dimensionless_quantity">
      <xs:attribute name="unit" type="xs:string" use="optional"/>
      <xs:attribute name="piece" type="xs:string" use="optional"/>
      <xs:attribute name="qualifier" type="xs:string" use="optional"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

<xs:simpleType name="dimensionless_quantity">
  <xs:restriction base="xs:string">
    <xs:pattern value="[\s0-9\.\-/]+"/>
  </xs:restriction>
</xs:simpleType>

<xs:complexType name="steps">
  <xs:sequence>
    <xs:element name="step" type="step"
                minOccurs="1" maxOccurs="unbounded"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="step">
  <xs:sequence>
    <xs:element name="link" type="link" minOccurs="0" maxOccurs="1"/>
    <xs:element name="instructions" type="xs:string"
                minOccurs="1" maxOccurs="1"/>
  </xs:sequence>
  <xs:attribute name="duration" type="xs:duration" use="optional"/>
  <xs:attribute name="type" type="xs:string" use="optional"/>
</xs:complexType>

<xs:complexType name="link">
  <xs:attribute name="mode" type="mode"/>
  <xs:attribute name="step" type="xs:positiveInteger"/>
</xs:complexType>

<xs:simpleType name="mode">
  <xs:restriction base="xs:string">
    <xs:enumeration value="starts after"/>
    <xs:enumeration value="starts in parallel to"/>
    <xs:enumeration value="ends with"/>
  </xs:restriction>
</xs:simpleType>

<xs:complexType name="change_history">
  <xs:sequence>
    <xs:element name="last_updated" type="authoring_date"
                minOccurs="0" maxOccurs="1"/>
    <xs:element name="recorded" type="authoring_date"
                minOccurs="0" maxOccurs="1"/>
    <xs:element name="source" type="xs:string" minOccurs="0" maxOccurs="1"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="authoring_date">
  <xs:attribute name="on" type="xs:dateTime"/>
</xs:complexType>

</xs:schema>

