<?xml version="1.0" encoding="UTF-8"?>
<grammar ns="tag:textalign.net,2015:ns" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" xmlns:local="tag:textalign.net,2015:ns" xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <start>
    <element name="TAN-A-div">
      <a:documentation>specifies that the file is a div-based TAN alignment file. Root element.</a:documentation>
      <ref name="TAN-root"/>
    </element>
  </start>
  <include href="incl/TAN-class-2.rng">
    <define name="source-list">
      <a:documentation>TAN-A-div files must have one or more sources</a:documentation>
      <oneOrMore>
        <ref name="source-item"/>
      </oneOrMore>
    </define>
    <define name="decl-id-ref-opt">
      <a:documentation>declarations by default have ids</a:documentation>
      <ref name="internal-id"/>
    </define>
    <define name="source-id-opt">
      <a:documentation>TAN-A-div sources must be named</a:documentation>
      <ref name="internal-id"/>
    </define>
    <define name="declaration-items">
      <interleave>
        <zeroOrMore>
          <ref name="decl-tok-def"/>
        </zeroOrMore>
        <zeroOrMore>
          <ref name="decl-supp-div-type"/>
        </zeroOrMore>
        <zeroOrMore>
          <ref name="decl-rename-div-n"/>
        </zeroOrMore>
        <ref name="TAN-c-decl-core"/>
      </interleave>
    </define>
    <define name="continuation-opt">
      <empty/>
    </define>
    <define name="tok-source-ref-opt">
      <empty/>
    </define>
    <define name="tok-cert-opt">
      <empty/>
    </define>
    <define name="TAN-body-core">
      <a:documentation>Redefining TAN-body-core not only defines what is in the body of a TAN-A-div but also excludes groups from it.</a:documentation>
      <zeroOrMore>
        <ref name="work-equiv"/>
      </zeroOrMore>
      <zeroOrMore>
        <ref name="div-type-equiv"/>
      </zeroOrMore>
      <zeroOrMore>
        <ref name="split"/>
      </zeroOrMore>
      <zeroOrMore>
        <ref name="realignment"/>
      </zeroOrMore>
      <optional>
        <ref name="set-of-claims"/>
      </optional>
    </define>
  </include>
  <!-- <equate-works> -->
  <define name="work-ref">
    <attribute name="work">
      <a:documentation>refers to a work by means of a source ID as a proxy. The attribute will be treated as indicating all sources that share the same work as the one mentioned.</a:documentation>
      <a:documentation>If you wish to avoid making a claim applying to all other versions of a work, use @src instead.</a:documentation>
      <data type="string">
        <param name="pattern">\S+</param>
      </data>
    </attribute>
  </define>
  <define name="work-refs">
    <attribute name="work">
      <a:documentation>refers to works by means of source IDs as a proxy. </a:documentation>
    </attribute>
  </define>
  <define name="work-equiv">
    <element name="equate-works">
      <a:documentation>declares an ad hoc equivalence between works that are not defined by the &lt;IRI&gt;s in their sources as being identical.</a:documentation>
      <a:documentation>This element extends the automatic equating of works, which is transitive and greedy. If work A is defined with &lt;IRI&gt; X, work B with &lt;IRI&gt;s X and Y, and work C with only &lt;IRI&gt; Y, then works A and C will be automatically equated.</a:documentation>
      <a:documentation>&lt;equate-works&gt; does not imply that the two works are, in reality, one and the same. It merely states that, for the purposes of this alignment, they should be treated as equivalent.</a:documentation>
      <optional>
        <ref name="ed-stamp"/>
      </optional>
      <choice>
        <ref name="inclusion"/>
        <ref name="work-refs"/>
      </choice>
    </element>
  </define>
  <!-- <equate-div-types> -->
  <define name="div-type-equiv">
    <element name="equate-div-types">
      <a:documentation>declares an ad hoc equivalence between div types that are not defined by the &lt;IRI&gt; values in their sources as being identical.</a:documentation>
      <a:documentation>&lt;equate-div-types&gt; are assumed to be greedy and transitive. If this element is used to equate div type X with type Y, then any div type in any source identical to X's or Y's IRI values will treated as identical. </a:documentation>
      <a:documentation>This element does not imply that the two types of division are, in reality, one and the same. It merely states that, for the purposes of this alignment, they should be treated as equivalent.</a:documentation>
      <optional>
        <ref name="ed-stamp"/>
      </optional>
      <choice>
        <ref name="inclusion"/>
        <group>
          <ref name="div-type-ref-cluster"/>
          <oneOrMore>
            <ref name="div-type-ref-cluster"/>
          </oneOrMore>
        </group>
      </choice>
    </element>
  </define>
  <define name="div-type-ref-cluster">
    <element name="div-type-ref">
      <a:documentation>points to a &lt;div-type&gt; in one or more sources, using the @xml:id assigned by the source to that div type. </a:documentation>
      <optional>
        <ref name="ed-stamp"/>
      </optional>
      <ref name="source-refs"/>
      <ref name="div-type-ref"/>
    </element>
  </define>
  <!-- <split-leaf-div-at> -->
  <define name="split">
    <element name="split-leaf-div-at">
      <a:documentation>creates ad hoc splits in leaf &lt;div&gt;s, to facilitate alignments and realignments of textual units smaller than leaf &lt;div&gt;s. Any leaf div may be split as many times as there are token, as defined by &lt;tokenization&gt;s.</a:documentation>
      <a:documentation>Each split creates a provisional segment, a textual subdivision of a leaf &lt;div&gt;.</a:documentation>
      <optional>
        <ref name="ed-stamp"/>
      </optional>
      <choice>
        <ref name="inclusion"/>
        <group>
          <ref name="source-refs"/>
          <interleave>
            <zeroOrMore>
              <ref name="comment"/>
            </zeroOrMore>
            <oneOrMore>
              <ref name="tok-regular"/>
            </oneOrMore>
          </interleave>
        </group>
      </choice>
    </element>
  </define>
  <!-- <realign> -->
  <define name="anchor-div-ref-item">
    <element name="anchor-div-ref">
      <a:documentation>refers to a group of one or more &lt;div&gt;s (or segments of &lt;div&gt;s) to which &lt;div&gt;s from other versions of the same work should be realigned (defined by the &lt;div-ref&gt;s that follow). The first child of a &lt;realign&gt;, it is constructed exactly like &lt;div-ref&gt;, except that only @src, not @work, is used, and @seg is excluded. </a:documentation>
      <a:documentation>&lt;anchor-div-ref&gt; does not permit the comma in @ref in an undistributed realignment, since realignment must always be made to a contiguous range of text. </a:documentation>
      <a:documentation>If the parent &lt;realign&gt;'s @distribute is false, or missing, then each &lt;div-ref&gt; group will be realigned as a whole to the anchor, treated as a whole.</a:documentation>
      <a:documentation>If @distribute is true, then every nth realign head will serve as the anchor for the nth realign head in each subsequent &lt;div-ref&gt;s (grouped by source).</a:documentation>
      <a:documentation>@seg, which applies to splits in specific sources, is not allowed because the job of an anchor is to reconcile other &lt;div&gt;s to a reference that is valid independent of any particular edition. @seg would defeat that purpose.</a:documentation>
      <a:documentation>For more, see &lt;div-ref&gt;.</a:documentation>
      <optional>
        <ref name="ed-stamp"/>
      </optional>
      <ref name="source-ref"/>
      <ref name="pointer-to-div-range"/>
    </element>
  </define>
  <define name="seg-ref">
    <attribute name="seg">
      <a:documentation>picks specific segments in a leaf div. There must be an appropriate number of splits declared for those leaf divs in &lt;split-leaf-div-at&gt;.</a:documentation>
      <ref name="seq-picker"/>
    </attribute>
  </define>
  <define name="reanchor-div-ref-item">
    <element name="div-ref">
      <a:documentation>refers to and groups one or more &lt;div&gt;s (or segments of &lt;div&gt;s).</a:documentation>
      <a:documentation>&lt;div-ref&gt;s are expanded against @src/@work, @ref, and @seg. That is, a &lt;div-ref&gt; points to every segment of every div of every source cited.</a:documentation>
      <a:documentation>A &lt;div-ref&gt;, or a group of &lt;div-ref&gt;s joined by @cont, are treated as many groups as sources referred to. That is, &lt;div-ref src="X Y" ... /&gt; will be treated as shorthand for &lt;div-ref src="X" ... /&gt; and &lt;div-ref src="Y" ... /&gt;. This applies to @work as well: &lt;div-ref work="w" ... /&gt; is equivalent to &lt;div-ref src="w1" ... /&gt;, &lt;div-ref src="w2" ... /&gt;, etc. </a:documentation>
      <a:documentation>After this distinction between sources is made, the entire set of &lt;div&gt;s pointed to will be treated as a group, and processed as a whole (see @distribute). </a:documentation>
      <a:documentation>&lt;div-ref&gt; is a grouping device, and is therefore unlike &lt;tok&gt;, which always refers to single items, never sets. As a result, the siblings &lt;div-ref src="X" ref="a"/&gt; and &lt;div-ref src="X" ref="b"/&gt; are NOT identical to &lt;div-ref src="X" ref="a, b"/&gt;   </a:documentation>
      <optional>
        <ref name="ed-stamp"/>
      </optional>
      <ref name="source-refs"/>
      <ref name="pointer-to-div-range"/>
      <optional>
        <ref name="seg-ref"/>
      </optional>
    </element>
  </define>
  <define name="realignment">
    <element name="realign">
      <a:documentation>corrects misaligned or unaligned divisions and segments in versions of the same work. &lt;realign&gt; is intended to reconcile discordant reference systems by mapping &lt;div&gt;s from one or more sources onto the &lt;div&gt;s of another, treated as an anchor. The reanchored divs are then excluded from any default alignments, but they inherit the anchor's alignments. </a:documentation>
      <a:documentation>Realignments come in four types.</a:documentation>
      <a:documentation>An UNANCHORED, ONE-SOURCE realigment consists solely of one or more &lt;div-ref&gt;s belonging to only one source. Any number of &lt;div&gt;s may be referred to by the child &lt;div-ref&gt;s. All &lt;div&gt;s referred to will be excluded from automatic alignment with every other version of the same work.</a:documentation>
      <a:documentation>An UNANCHORED, MULTIPLE-SOURCE realignment realigns &lt;div&gt;s from multiple sources. The number of &lt;div&gt;s per source must be identical, since these &lt;div&gt;s will not only be excluded from automatic alignment, but realigned with each other, with the nth &lt;div&gt; in each source constituting a realigned group.</a:documentation>
      <a:documentation>An ANCHORED, ONE-TO-ONE realignment consists of an &lt;anchor-div-ref&gt; that points to n &lt;div&gt;s, and each source invoked by the following &lt;div-ref&gt;s also point to n &lt;div&gt;s, which will be reanchored to the nth &lt;div&gt; in the anchor. </a:documentation>
      <a:documentation>An ANCHORED, ONE-TO-MANY realignment consists of an &lt;anchor-div-ref&gt; that points only to one &lt;div&gt;, and &lt;div-ref&gt;s that point to any number of sources and any number of &lt;div&gt;s. To that one anchor will be remapped each source's collection of &lt;div&gt;s to be reanchored. The sequence of &lt;div&gt;s referred to will be respected. If to an anchor A is remapped &lt;div&gt;s 2, 4, and 3, then  </a:documentation>
      <a:documentation>Realignment is disruptive, displacing any default alignment that might have been inferred in the reanchored source. Assume a case where a &lt;div&gt; with reference B is being realigned to an anchor with reference A. In the source to be reanchored reference A, if it exists, will be excluded from automatic alignment with the anchor, unless if it is explicitly included in a &lt;realign&gt;. </a:documentation>
      <a:documentation>All realignment, except one-to-many anchored, is inheritable, affecting not only the specified &lt;div&gt;s but also their descendants. That is, if X is realigned to Y, then children in X will be automatically aligned with children in Y, based on normalized values of @n. Children of &lt;div&gt;s in one-to-many anchored realignments must be explicitly realigned.</a:documentation>
      <a:documentation>Because of the distributive nature of the realignment, every value of @ref that involves a hyphen must have siblings with balanced depth on the left and right side of the hyphen (i.e., ref="1 - 2.1" would be invalid).</a:documentation>
      <a:documentation>&lt;realign&gt; does not affect subsequent nomenclature of @ref. If in source x a &lt;div&gt; A is realigned with &lt;div&gt; B in source y, then subsequent references to the realigned &lt;div&gt; in source x should still use A as a value of @ref, not B.  </a:documentation>
      <a:documentation>The order of &lt;realign&gt;s is immaterial. No &lt;realign&gt; overwrites any other &lt;realign&gt;. If two &lt;realign&gt;s each reanchor A and B in the same source to C in the anchor, then A and B will be realigned in parallel. If one meant to say that A followed by B is to be realigned to C, then a one-to-many anchored realignment should be used.  </a:documentation>
      <optional>
        <ref name="ed-stamp"/>
      </optional>
      <choice>
        <ref name="inclusion"/>
        <interleave>
          <zeroOrMore>
            <ref name="comment"/>
          </zeroOrMore>
          <group>
            <zeroOrMore>
              <ref name="anchor-div-ref-item"/>
            </zeroOrMore>
            <oneOrMore>
              <ref name="reanchor-div-ref-item"/>
            </oneOrMore>
          </group>
        </interleave>
      </choice>
    </element>
  </define>
  <!-- <claim> -->
  <include href="incl/TAN-c-core.rng">
    <define name="textual-reference">
      <choice>
        <ref name="simple-textual-reference"/>
        <ref name="complex-textual-reference-set"/>
      </choice>
    </define>
  </include>
  <define name="set-of-claims">
    <ref name="claimant"/>
    <optional>
      <ref name="subject"/>
    </optional>
    <optional>
      <ref name="verb"/>
    </optional>
    <oneOrMore>
      <ref name="claim"/>
    </oneOrMore>
  </define>
  <define name="simple-textual-reference">
    <choice>
      <ref name="source-refs"/>
      <ref name="work-ref"/>
    </choice>
    <ref name="pointer-to-div-range"/>
    <optional>
      <ref name="seg-ref"/>
    </optional>
  </define>
  <define name="complex-textual-reference-set">
    <choice>
      <ref name="source-refs"/>
      <ref name="work-ref"/>
    </choice>
    <oneOrMore>
      <ref name="complex-text-ref"/>
    </oneOrMore>
  </define>
  <define name="complex-text-ref">
    <choice>
      <ref name="claim-div-ref-item"/>
      <ref name="tok-regular"/>
    </choice>
  </define>
  <define name="claim-div-ref-item">
    <element name="div-ref">
      <optional>
        <ref name="ed-stamp"/>
      </optional>
      <ref name="pointer-to-div-range"/>
      <optional>
        <ref name="seg-ref"/>
      </optional>
    </element>
  </define>
</grammar>
