<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="https://relaxng.org/relaxng.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>

<!--
  Shared biblStruct Definition Fragment

  This schema fragment defines the biblStruct element and all its child elements
  according to TEI guidelines, adapted for GROBID training schemas.

  It can be referenced from other schemas using <externalRef> to avoid duplication.

  Structure:
  - biblStruct: Container for structured bibliographic citations
    - analytic (0+): Article/chapter-level information
    - monogr (1+): Monograph/journal-level information
    - series (0+): Series information
    - Trailing elements: note, ptr, ref, idno
-->
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
  xmlns:xml="http://www.w3.org/XML/1998/namespace"
  ns="http://www.tei-c.org/ns/1.0"
  datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">

  <start>
    <ref name="biblStruct"/>
  </start>

  <!-- ============================================================ -->
  <!-- Main biblStruct Definition                                    -->
  <!-- ============================================================ -->

  <define name="biblStruct">
    <element name="biblStruct">
      <optional>
        <attribute name="type"/>
      </optional>
      <optional>
        <attribute name="id" ns="http://www.w3.org/XML/1998/namespace"/>
      </optional>
      <group>
        <!-- Zero or more analytic elements (for articles, chapters, etc.) -->
        <zeroOrMore>
          <ref name="analytic"/>
        </zeroOrMore>
        <!-- One or more monogr elements (for books, journals, etc.) -->
        <oneOrMore>
          <ref name="monogr"/>
          <!-- Each monogr can optionally be followed by series elements -->
          <zeroOrMore>
            <ref name="series"/>
          </zeroOrMore>
        </oneOrMore>
        <!-- Trailing elements -->
        <zeroOrMore>
          <choice>
            <ref name="note"/>
            <ref name="ptr"/>
            <ref name="ref"/>
            <ref name="idno"/>
          </choice>
        </zeroOrMore>
      </group>
    </element>
  </define>

  <!-- ============================================================ -->
  <!-- Bibliographic Level Elements                                  -->
  <!-- ============================================================ -->

  <define name="analytic">
    <element name="analytic">
      <zeroOrMore>
        <choice>
          <ref name="author"/>
          <ref name="editor"/>
          <ref name="title"/>
          <ref name="meeting"/>
          <ref name="date"/>
          <ref name="idno"/>
          <ref name="ptr"/>
          <ref name="ref"/>
          <ref name="availability"/>
        </choice>
      </zeroOrMore>
    </element>
  </define>

  <define name="monogr">
    <element name="monogr">
      <zeroOrMore>
        <choice>
          <ref name="author"/>
          <ref name="editor"/>
          <ref name="title"/>
          <ref name="imprint"/>
          <ref name="biblScope"/>
          <ref name="edition"/>
          <ref name="date"/>
          <ref name="idno"/>
          <ref name="ptr"/>
          <ref name="ref"/>
          <ref name="availability"/>
          <ref name="note"/>
        </choice>
      </zeroOrMore>
    </element>
  </define>

  <define name="series">
    <element name="series">
      <interleave>
        <text/>
        <zeroOrMore>
          <choice>
            <ref name="title"/>
            <ref name="editor"/>
            <ref name="biblScope"/>
            <ref name="idno"/>
            <ref name="availability"/>
          </choice>
        </zeroOrMore>
      </interleave>
    </element>
  </define>

  <!-- ============================================================ -->
  <!-- Responsibility Elements (Authors, Editors)                    -->
  <!-- ============================================================ -->

  <define name="author">
    <element name="author">
      <optional>
        <attribute name="role"/>
      </optional>
      <optional>
        <attribute name="id" ns="http://www.w3.org/XML/1998/namespace"/>
      </optional>
      <interleave>
        <text/>
        <zeroOrMore>
          <choice>
            <ref name="persName"/>
            <ref name="forename"/>
            <ref name="surname"/>
            <ref name="middlename"/>
          </choice>
        </zeroOrMore>
      </interleave>
    </element>
  </define>

  <define name="editor">
    <element name="editor">
      <optional>
        <attribute name="role"/>
      </optional>
      <optional>
        <attribute name="id" ns="http://www.w3.org/XML/1998/namespace"/>
      </optional>
      <interleave>
        <text/>
        <zeroOrMore>
          <choice>
            <ref name="persName"/>
            <ref name="forename"/>
            <ref name="surname"/>
            <ref name="middlename"/>
          </choice>
        </zeroOrMore>
      </interleave>
    </element>
  </define>

  <!-- ============================================================ -->
  <!-- Personal Name Elements                                        -->
  <!-- ============================================================ -->

  <define name="persName">
    <element name="persName">
      <optional>
        <attribute name="id" ns="http://www.w3.org/XML/1998/namespace"/>
      </optional>
      <interleave>
        <text/>
        <zeroOrMore>
          <ref name="forename"/>
        </zeroOrMore>
        <zeroOrMore>
          <ref name="surname"/>
        </zeroOrMore>
        <zeroOrMore>
          <ref name="middlename"/>
        </zeroOrMore>
      </interleave>
    </element>
  </define>

  <define name="forename">
    <element name="forename">
      <optional>
        <attribute name="type"/>
      </optional>
      <text/>
    </element>
  </define>

  <define name="surname">
    <element name="surname">
      <optional>
        <attribute name="type"/>
      </optional>
      <text/>
    </element>
  </define>

  <define name="middlename">
    <element name="middlename">
      <optional>
        <attribute name="type"/>
      </optional>
      <text/>
    </element>
  </define>

  <!-- ============================================================ -->
  <!-- Title Element                                                 -->
  <!-- ============================================================ -->

  <define name="title">
    <element name="title">
      <optional>
        <attribute name="type"/>
      </optional>
      <optional>
        <attribute name="level">
          <choice>
            <value>a</value>  <!-- analytic -->
            <value>m</value>  <!-- monographic -->
            <value>j</value>  <!-- journal -->
            <value>s</value>  <!-- series -->
            <value>u</value>  <!-- unpublished -->
          </choice>
        </attribute>
      </optional>
      <text/>
    </element>
  </define>

  <!-- ============================================================ -->
  <!-- Imprint and Publication Elements                              -->
  <!-- ============================================================ -->

  <define name="imprint">
    <element name="imprint">
      <zeroOrMore>
        <choice>
          <ref name="publisher"/>
          <ref name="pubPlace"/>
          <ref name="date"/>
        </choice>
      </zeroOrMore>
    </element>
  </define>

  <define name="publisher">
    <element name="publisher">
      <optional>
        <attribute name="id" ns="http://www.w3.org/XML/1998/namespace"/>
      </optional>
      <text/>
    </element>
  </define>

  <define name="pubPlace">
    <element name="pubPlace">
      <text/>
    </element>
  </define>

  <!-- ============================================================ -->
  <!-- Bibliographic Scope and Range Elements                        -->
  <!-- ============================================================ -->

  <define name="biblScope">
    <element name="biblScope">
      <optional>
        <attribute name="unit"/>
      </optional>
      <optional>
        <attribute name="from"/>
      </optional>
      <optional>
        <attribute name="to"/>
      </optional>
      <text/>
    </element>
  </define>

  <!-- ============================================================ -->
  <!-- Date and Identifier Elements                                  -->
  <!-- ============================================================ -->

  <define name="date">
    <element name="date">
      <optional>
        <attribute name="type"/>
      </optional>
      <optional>
        <attribute name="when"/>
      </optional>
      <text/>
    </element>
  </define>

  <define name="idno">
    <element name="idno">
      <optional>
        <attribute name="type"/>
      </optional>
      <text/>
    </element>
  </define>

  <!-- ============================================================ -->
  <!-- Edition and Availability Elements                             -->
  <!-- ============================================================ -->

  <define name="edition">
    <element name="edition">
      <interleave>
        <text/>
        <zeroOrMore>
          <choice>
            <ref name="date"/>
            <ref name="idno"/>
          </choice>
        </zeroOrMore>
      </interleave>
    </element>
  </define>

  <define name="availability">
    <element name="availability">
      <optional>
        <attribute name="status"/>
      </optional>
      <text/>
    </element>
  </define>

  <!-- ============================================================ -->
  <!-- Note and Reference Elements                                   -->
  <!-- ============================================================ -->

  <define name="note">
    <element name="note">
      <optional>
        <attribute name="type"/>
      </optional>
      <optional>
        <attribute name="place"/>
      </optional>
      <text/>
    </element>
  </define>

  <define name="ptr">
    <element name="ptr">
      <attribute name="target"/>
      <empty/>
    </element>
  </define>

  <define name="ref">
    <element name="ref">
      <optional>
        <attribute name="target"/>
      </optional>
      <text/>
    </element>
  </define>

  <!-- ============================================================ -->
  <!-- Meeting Element (for conference proceedings)                  -->
  <!-- ============================================================ -->

  <define name="meeting">
    <element name="meeting">
      <optional>
        <attribute name="id" ns="http://www.w3.org/XML/1998/namespace"/>
      </optional>
      <text/>
    </element>
  </define>

</grammar>
