<?xml version="1.0" encoding="UTF-8"?>
   
<grammar xmlns="http://relaxng.org/ns/structure/1.0">

<start>
  <!-- Reel defined here. There will be 42 reels 42 xml files when completed. -->
  <element name="reel" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <attribute name="number">
    	<text/>
    </attribute>
    			
    <oneOrMore>
            <element name="record" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
                    <attribute name="id"/>
                    <attribute name="color">
                        <choice>
                            <value>1pink</value>
                            <value>2yellow</value>
                            <value>3white</value>
                            <value>4blue</value>
                            <value>5goldenrod</value>
                        </choice>
                    </attribute>
                    <optional>
                    	<attribute name="r"><text/></attribute>
                    </optional>
                    <optional>
                    	<attribute name="z"><text/></attribute>
                    </optional>
            	<optional>
            		<attribute name="y"/>
            	</optional>
            	<optional>
            		<attribute name="series">
            			<choice>
            				<value>2</value>
            				<value>3</value>
            			</choice>
            		</attribute>
            	</optional>
            	<optional>
                        <attribute name="language">
                            <text/>
                        </attribute>
                    </optional>
                    <ref name="record-contents"/>
            </element>
    </oneOrMore>
  </element>
 </start>

    <define name="record-contents">

        <!-- DATE, can include exact, ranges, ante and post, and conjectural in brackets -->
        <interleave>
            <oneOrMore>
                <!-- <date> changed to oneOrMore by Mike Rush, 2009-08-02 -->
                <!-- 2010-03-25 this allows to encode multiple dates that may appear in journal letters or diary entries, doesn't happen that often -MTC -->
                <element name="date">
                    <interleave>
                        <text/>
                        <choice>
                            <attribute name="when">
                                <ref name="date-param"/>
                            </attribute>
                            <attribute name="unknown">
                                <value>unknown</value>
                            </attribute>
                            <group>
                                <attribute name="when">
                                    <ref name="date-param"/>
                                </attribute>
                                <attribute name="ante">
                                    <value>ante</value>
                                </attribute>
                            </group>
                            <group>
                                <attribute name="when">
                                    <ref name="date-param"/>
                                </attribute>
                                <attribute name="post">
                                    <value>post</value>
                                </attribute>
                            </group>
                            <group>
                                <attribute name="when">
                                    <ref name="date-param"/>
                                </attribute>
                                <attribute name="to">
                                    <ref name="date-param"/>
                                </attribute>
                            </group>
                        </choice>
                        <!-- 2010-03-25 this date construction seems to be working well -MTC -->
                        <zeroOrMore>
                            <choice>
                                <attribute name="circa"/>
                                <attribute name="noDate"/>
                                <attribute name="conjectural"/>
                            </choice>
                        </zeroOrMore>
                        <optional>
                            <attribute name="rank"/>
                        </optional>
                    </interleave>
                </element>
            </oneOrMore>

            <!-- PLACE  -->
            <optional>
                <element name="places">
                        <interleave>
                            <text/>
                            <zeroOrMore>
                                <ref name="reference-element"/>
                            </zeroOrMore>
                        </interleave>
                </element>
            </optional>

 
            <optional>
                <choice>
                    <element name="code">
                        <attribute name="type">
                            <value>accession</value>
                        </attribute>
                         <attribute name="institution">
                                <text/>
                         </attribute>
                        <optional>
                            <attribute name="privateowner">
                                <text/>
                            </attribute>
                        </optional>
                        <attribute name="number"/>
                        <text/>
                    </element>
                    <element name="code">
                        <attribute name="type">
                            <value>letterbook</value>
                        </attribute>
                        <attribute name="number">
                            <text/>
                        </attribute>
                        <optional>
                            <attribute name="author">
                                <text/>
                            </attribute>
                        </optional>
                        <text/>
                    </element>
                    <element name="code">
                        <attribute name="type">
                            <value>diary</value>
                        </attribute>
                        <attribute name="author">
                            <text/>
                        </attribute>
                        <attribute name="number">
                            <text/>
                        </attribute>
                        <text/>
                    </element>
                    <element name="code">
                        <attribute name="type">
                            <value>miscellany</value>
                        </attribute>
                        <attribute name="author">
                            <text/>
                        </attribute>
                        <attribute name="number">
                            <text/>
                        </attribute>
                        <text/>
                    </element>
                    <element name="code">
                        <attribute name="type">
                            <value>general</value>
                        </attribute>
                        <text/>
                    </element>
                </choice>
            </optional>

            <!-- RECIPIENT, will always begin with the word "to"; can include a nested reference tag to a target attribute that will correspond to names database -->
            <optional>
                <element name="recipient">
                    <interleave>
                        <text/>
                        <zeroOrMore>
                            <ref name="reference-element"/>
                        </zeroOrMore>
                    </interleave>
                </element>
            </optional>

            <!-- LENGTH -->
            <optional>
                <element name="length">
                    <text/>
                    <attribute name="pages">
                        <text/>
                    </attribute>
                </element>
            </optional>

            <!-- COPY (FORMAT) -->
            <optional>
                <element name="copy">
                     <attribute name="format">
                        <choice>
                            <value>Photocopy</value>
                            <value>Manuscript</value>
                            <value>Microfilm</value>
                            <value>Digital Image</value>
                        </choice>
                    </attribute>
 		<interleave>
 			<text/>
 			<zeroOrMore>
 				<ref name="reference-element"/>
 			</zeroOrMore>
 		</interleave>
                </element>
            </optional>

             <optional>
                <element name="printed">
                    <interleave>
                        <text/>
                        <zeroOrMore>
                            <ref name="reference-element"/>
                        </zeroOrMore>
                    </interleave>
                    </element>
            </optional>

            <!-- NOTES can appear many times or not at all; the most common notes will be coded as collection (mostly appearing on 
            yellow accession records), auction (mostly appearing on blue lead records, or internal (mostly the initials and dates of 
            editors who created the slip or private owner information that should not be displayed for public -->

            <optional>
                    <element name="notes">
                      <oneOrMore>
                       <element name="note">
                        <interleave>
                            <text/>
                            <zeroOrMore>
                                <ref name="reference-element"/>
                            </zeroOrMore>
                             <zeroOrMore>
                                <element name="internal">
                                    <text/>
                                </element>
                            </zeroOrMore>
                        </interleave>
                       </element>
                      </oneOrMore>
                    </element>
           </optional>

            <!-- Subject headings will be add later when in eXist database; will be searched and added against paper file -->
            <optional>
                <element name="docType">
                    <text/>
                </element>
            </optional>


            <!-- SERIES tags can only be one or the other -->
            <optional>
                <element name="series">
                    <choice>
                        <value>II</value>
                        <value>III</value>
                    </choice>
                </element>
            </optional>

            <!-- Microfilm and Adams Electronic Archive links may be added once the data is moved into eXist-->

            <optional>
                <!-- During Phase 3 the apmicrofilm tag should be required of every record, it is optional now so that the files validate -->
                <element name="apmicrofilm">
                    <attribute name="reel">
                        <text/>
                    </attribute>
                </element>
            </optional>
        	
            <optional>
                <element name="Earchive">
                    <attribute name="link">
                        <text/>
                    </attribute>
                </element>
            </optional>


            <!-- First choice is to have an AUTHOR -->
            <choice>
                <group>
                    <ref name="author-element"/>
                </group>


                <!-- Second choice is to have a TITLE -->
                <group>
                    <ref name="title-element"/>
                </group>


                <!-- Third choice is to have either an AUTHOR and/or a TITLE. Each record MUST have at least an AUTHOR or a TITLE, and can have both -->
                <group>
                    <interleave>
                        <ref name="author-element"/>
                        <ref name="title-element"/>
                    </interleave>
                </group>
            </choice>
        </interleave>
    </define>

    <!-- The ref tag here can link to either the names database (which will include persons, corporate (groups) or offices) or to 
        the Short Title database -->
    <define name="reference-element">
        <element name="ref">
                <attribute name="type">
                    <choice>
                        <value>person</value>
                        <value>short-title</value>
                        <value>location</value>
                        <value>link</value>
                    	  <value>record</value>
                    </choice>
                </attribute>
                <attribute name="target">
                    <text/>
                </attribute>
                <optional>
                    <attribute name="linkType">
                        <text/>
                    </attribute>
                </optional>
	<text/>
        </element>
    </define>


    <define name="author-element">
        <element name="author">
            <interleave>
                <text/>
                <zeroOrMore>
                    <ref name="reference-element"/>
                </zeroOrMore>
            </interleave>
        </element>
    </define>

    <define name="title-element">
        <element name="title">
            <interleave>
                <text/>
                <zeroOrMore>
                    <ref name="reference-element"/>
                </zeroOrMore>
            </interleave>
        </element>
    </define>


    <define name="date-param" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
        <data type="string">
            <param name="pattern"
                >1([6-9])([0-9]{2})(\-(01|02|03|04|05|06|07|08|09|10|11|12|99|00)(\-((0[1-9])|((1|2)[0-9])|(3[0-1])|(99)|(00))))</param>
        </data>
        <!--<choice>
            <data type="date">
                <param name="minInclusive">1600-01-01</param>
                <param name="maxInclusive">1900-12-31</param>
                <param name="pattern"
                    >1([6-9])([0-9]{2})(\-(01|02|03|04|05|06|07|08|09|10|11|12)(\-((0[1-9])|((1|2)[0-9])|(3[0-1])))?)?</param>
            </data>
            <data type="gYear">
                <param name="minInclusive">1600</param>
                <param name="maxInclusive">1900</param>
            </data>
            <data type="gYearMonth">
                <param name="minInclusive">1600-01</param>
                <param name="maxInclusive">1900-12</param>
            </data>
        </choice>-->

    </define>



</grammar>
