<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2" xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
    <title>schematron for Ahikar project</title>    
    <ns uri="http://www.tei-c.org/ns/1.0" prefix="tei"/>
    
    <pattern>
        <rule context="@xml:id">
            <report test="matches(., '[äöüÄÖÜß]')">
                xml:id must not contain umlauts.
            </report>
        </rule>
    </pattern>
    
    <pattern>
        <rule context="@xml:lang">
            <report test="not(matches(., 'eng|ara|syc|karshuni|syc-syrj|syc-syrn|lat|sog|aii'))">
                @xml:lang must be one of 
                eng
                ara
                syr
                karshuni
                syc-syrj
                syc-syrn
                lat
                sog
                aii
            </report>
        </rule>
    </pattern>
    
    
    <pattern>
        <rule context="tei:orig">
            <assert test="parent::tei:damage or parent::tei:choice">
                orig is allowed within tei:damage or tei:choice only. think about using tei:sic with tei:corr here.
            </assert>
        </rule>
    </pattern>
    
    
  <!-- This rule has been commented out in favour of the more complex pb/@n of the Sogdian fragments. -->
  <!--  <pattern>
        <rule context="tei:pb/@n">
            <report test="not(matches(., '^\d+(a|b|r|v)?'))">
                tei:pb/@n should start with digits followed by one of a, b, r or v.
            </report>
        </rule>
    </pattern>
  -->
    
    <pattern>
        <rule context="tei:text[@type='transcription']//tei:pb">
            <report test="not(exists(./@facs))">
                tei:pb/@facs every page beginning should refere to an image. Please insert the attribute "facs".
            </report>
        </rule>
    </pattern>
    <pattern>
        <rule context="tei:text[not(@type='translation')]//tei:pb/@facs">
            <report test="not(matches(., '^textgrid:'))">
                tei:pb/@facs should point to a textgrid resource starting with 'textgrid:'.
            </report>
        </rule>
    </pattern>
    <pattern>
        <rule context="tei:text[@type='translation']//tei:pb">
            <report test="exists(@facs)">
                please do not put @facs to translations when the named facsimile does not contain this translation 
            </report>
        </rule>
    </pattern>
    
    <pattern>
        <rule context="tei:TEI">
            <report test="count(tei:text[@type = 'transcription']) gt 1">
                Each document should have only one transcription.
            </report>
        </rule>
    </pattern>
    
    <pattern>
        <rule context="tei:pb">
            <let name="this" value="."/>
            <report test="@n = preceding::tei:pb[@facs][ancestor::tei:text[@type] = $this/ancestor::tei:text[@type]]/@n">
                This tei:pb's number has already been used for describing a facsimile. 
                Please check the value of @n.
            </report>
        </rule>
    </pattern>
    
 <!--   <pattern>
        <rule context="tei:choice">
            <report test="not(count(child::*) = 2) and not(count(child::supplied) = 2)">
                tei:choice should always have two child elements.
            </report>
        </rule>
    </pattern> -->
    
    <pattern>
        <rule context="tei:choice">
            <assert test="tei:sic and tei:corr or tei:abbr and tei:expan or tei:orig and tei:supplied">
                tei:choice must have either tei:sic and tei:corr or tei:abbr and tei:expan or tei:orig and tei:supplied as child nodes.
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Points out languages that are declared in the header but aren't used.</p>
        <rule context="tei:language/@ident">
            <assert test="//@xml:lang = .">
                This language isn't used in the document. Please remove it.
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Checks if the unit attribute of tei:milestone fits to an agreed domain.</p>
        <rule context="tei:milestone/@unit">
            <assert test=". = ('first_narrative_section', 'sayings', 'second_narrative_section', 'parables', 'third_narrative_section', 'minute')">
                "<value-of select="."/>" isn't allowed as value for unit. Please choose one of the following:
                first_narrative_section,
                sayings,
                second_narrative_section,
                parables,
                third_narrative_section
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>We use oXygen author comments for pointing out motifs in the text.
            They should conform to certain values to enable further processing in the front end.
            A complete overview of these IDs can be found at https://wiki.de.dariah.eu/display/AHK/List+of+Motivs.</p>
        <rule context="processing-instruction('oxy_comment_start')">
            <assert test='substring-before(substring-after(., "comment="""), """") 
                = ("successful_courtier",
                "loyal_obligation_gods",
                "keeping_secrets",
                "temptress_women",
                "parable_plants",
                "parable_animals",
                "social_contacts_no_family",
                "social_contacts_family",
                "wise_fool_sinful",
                "disciplining_of_sons",
                "discernment",
                "caution_against_powerful",
                "self_examination_reflection",
                "intrigue",
                "royal_challenges",
                "air_castle",
                "image_king",
                "treatment_slaves",
                "adherence_wisdom",
                "burden_debt",
                "richness_poverty",
                "truth_lying",
                "obeying_parents",
                "quarrel",
                "meekness_showing_respect",
                "beauty_caducity",
                "good_name",
                "death_age",
                "enemies")'>
                This motif isn't part of the Ahikar motifs list.
                Cf. https://wiki.de.dariah.eu/display/AHK/List+of+Motivs for a comprehensive list of possible IDs.
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Checks if the ID referenced by @next exists.</p>
        <rule context="@next">
            <assert test="//@xml:id = substring-after(., '#')">
                The xml:id referenced in @next doesn't exist in this document.
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Checks if the ID referenced by @prev exists.</p>
        <rule context="@prev">
            <assert test="//@xml:id = substring-after(., '#')">
                The xml:id referenced in @prev doesn't exist in this document.
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Asserts that each page number is used only once per text type (transcription/transliteration/tranlation.)</p>
        <rule context="tei:pb">
            <let name="this" value="."/>
            <assert test="not($this/@n = preceding-sibling::tei:pb/@n)">
                The page number encoded in @n is already in use.
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Asserts that the license we agreed on (CC BY-SA 4.0) is given in the correct form.</p>
        <rule context="tei:licence">
            <let name="this" value="."/>
            <assert test="@target = 'https://creativecommons.org/licenses/by-sa/4.0/'
                and normalize-space(./text()) = 'Distributed under a Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) License.'">
                Please provide the @target and description for tei:license.
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Checks if tei:p is only used within tei:abstract.</p>
        <rule context="tei:p">
            <report test="not(parent::tei:abstract)">
                tei:p can only be used as a direct child of tei:abstract.
            </report>
        </rule>
    </pattern>
    
    <pattern>
        <p>Asserts that only quotes/paraphrases that span over several lines omit tei:note and tei:bibl.</p>
        <rule context="tei:cit[(not(@type = 'verbatim'))]">
            <assert test="not(@next) and tei:note[@type = 'parallel-text']/tei:bibl
                or @next and not(tei:note[@type = 'parallel-text']/tei:bibl)">
                A quote or paraphrase needs a tei:bibl element as child of tei:note[@type = "parallel-text"]  if it doesn't have a @next attribute. 
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Asserts that only quotes/paraphrases that span over several lines omit tei:note and tei:bibl.</p>
        <rule context="tei:cit[@type = 'paraphrase']">
            <assert test="not(@next) and tei:note[@type = 'parallel-text']/tei:bibl and tei:note[@type = 'parallel-text']/tei:note
                or @next and not(tei:note[@type = 'parallel-text']/tei:bibl or tei:note[@type = 'parallel-text']/tei:note)">
                A paraphrase needs both a tei:bibl and a tei:note element as child of tei:note[@type = 'parallel-text'] if it doesn't have a @next attribute. 
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Checks if only the necessary tei:text elements are provided.</p>
        <rule context="tei:text">
            <assert test="normalize-space(string-join(descendant::text(), '')) != ''">
                This tei:text element is empty. Please remove it.
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Checks if the person responsible (expressed in @ref) is resolved.</p>
        <rule context="@resp">
            <assert test="//tei:editor/@xml:id = substring-after(., '#')
                or //tei:respStmt/@xml:id = substring-after(., '#')">
                There is no editor provided for this responsibility key. Please add one in the TEI header.
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Ensures that tei:corr, tei:abbr, tei:expan are used as a child of tei:choice only.</p>
        <rule context="tei:corr | tei:abbr | tei:expan">
            <assert test="parent::tei:choice">
                tei:<name/> can only be used as a child of tei:choice.
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <p>Checks if a word divided by a line break doesn't have white spaces at the end/beginning of the line.</p>
        <rule context="tei:ab">
            <report 
                test="following::tei:ab[1][child::tei:lb] and (starts-with(descendant::text()[matches(., '[\w]')][last()], ' ') or ends-with(descendant::text()[matches(., '[\w]')][last()], ' '))"
                role="warn">
                Please remove the white spaces at the end of the line before a split word.
            </report>
            <report 
                test="tei:lb and (starts-with(tei:lb/following::text()[1], ' ') or ends-with(tei:lb/following::text()[1], ' '))"
                role="warn">
                Please remove the white spaces at the beginning of the line with a split word.
            </report>
        </rule>
    </pattern>
    
    <pattern>
        <p>Checks if the ID of a motif has already been used.</p>
        <rule context="processing-instruction()">
            <let name="id" value="substring-after(., 'mid=')"/>
            <report test="$id and count(//processing-instruction('oxy_comment_start')[contains(., $id)]) gt 1">
                <value-of select="$id"/> has already been used as "mid" value. Make sure that <value-of select="$id"/> is only used once.
            </report>
            
        </rule>
    </pattern>
    
    <pattern>
        <p>Checks if all motifs are well-formed.</p>
        <rule context="tei:TEI">
            <let name="comment_starts" value="count(//processing-instruction('oxy_comment_start'))"/>
            <let name="comment_ends" value="count(//processing-instruction('oxy_comment_end'))"/>
            <report test="$comment_starts gt $comment_ends">
                There are motifs starting in this document for which no end could be detected.
                Check in oXygen's author mode or contact the SUB stuff for further help.
            </report>
            
            <report test="$comment_starts lt $comment_ends">
                There are motifs ending in this document for which no starting point could be detected.
                Check in oXygen's author mode or contact the SUB stuff for further help.
            </report>
        </rule>
    </pattern>
    
    <pattern>
        <p>Ensures correct global structure.</p>
        <rule context="tei:TEI/tei:text">
            <assert test="tei:group">
                Ahiqar resources must contain a tei:group in the
                first tei:text element. Add one to the first tei:text.
            </assert>
        </rule>
    </pattern>
    
    <pattern>
        <rule context="tei:group">
            <assert test="tei:text[@type = 'transcription']">
                A tei:group must contain at least a transcription.
            </assert>
        </rule>
    </pattern>
</schema>
