<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2"
    xmlns:sqf="http://www.schematron-quickfix.com/validator/process"
    xmlns="http://purl.oclc.org/dsdl/schematron">
    
<!--IN PROGRESS-->
<!-- This Schematron check conditions of a SvSal TEI doc for articles (Lemma) that must be met in order for the document 
    to be ready for publication. Such features include the correct tagging of citations <bibl>, <persName>, <biblScope> etc.-->
    
    <ns uri="http://www.tei-c.org/ns/1.0" prefix="tei"/>
    <sch:pattern id="bibl">
        <!--
            no elements insided bibl: //tei:body//tei:bibl[not(descendant::*)]
            persName in bibl without author-element: //tei:body//bibl[persName[not(parent::author)]]
        
        -->
        <sch:rule context="tei:bibl">
            <sch:assert test="descendant::* = (tei:author|tei:persName|tei:title|tei:date|tei:biblScope)"><!-- OK-->
                Error: Depending on the structure bibl should have persName, date and biblScope elements.
            </sch:assert>
            <sch:assert test="not(tei:persName[parent::tei:author])">
                Error: persName elements inside bibl elements should be also inside author element.
            </sch:assert>
            <sch:assert test="not(descendant::tei:persName[2])">
                Warning: 'bibl' element contains 2 'persName' elements.
            </sch:assert>
        </sch:rule>        
    </sch:pattern>

    <sch:pattern id="ref">
    <sch:rule context="tei:ref">
      <sch:assert test="not(tei:ref[ancestor::tei:p])">
        Warning: 'bibl' element contains  referencias (ref).
      </sch:assert>
    </sch:rule>
    <rule context="tei:ref">
        <assert test="@target">
            ERROR: ref elements must have @target.
        </assert>
    </rule>  
   </sch:pattern>

    <pattern id="div">
        <rule context="tei:div[not(@type = ('section', 'subsection', 'contents', 'sources'))]">
            <assert test="@type/string()">
                Warning: tei:div needs a correct @type.
            </assert>
        </rule>
        <rule context="tei:div[not(@type)]">
            <assert test="@type">
                WARN: tei:div need a @type.
            </assert>
        </rule>
        <rule context="tei:div[not(@n)]">
            <assert test="matches(@n, '\d')">
                WARN: tei:div need a @n.
            </assert>
        </rule>
        <rule context="tei:div">
            <assert test="not((.//text())[1]/ancestor::tei:head)">
                WARN: first text node within div is not within a heading - (tei:div usually has tei:head as first child).
            </assert>
        </rule>
    </pattern>
    <pattern id="IDS">
        <rule context="tei:body//(tei:div|tei:bibl|tei:quote|tei:p|tei:list|tei:item)">
            <assert test="@xml:id">
                ERROR: elements must have @xml:id.
            </assert>
        </rule>
    </pattern>

</sch:schema>