<?xml version="1.0" encoding="UTF-8"?>
<sch:pattern id="ISM-ID-00088" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
    <sch:p id="ruleText">
        [ISM-ID-00088][Error] If ISM-CAPCO-RESOURCE and any element in the document:
        1. Meets ISM-CONTRIBUTES
        AND
        2. Has the attribute disseminationControls NOT containing [REL]
        AND
        3. Has the attribute classification NOT having a value of [U]
        Then the ISM-RESOURCE-ELEMENT must NOT have attribute disseminationControls containing [REL].
        
        Human Readable: USA documents having any classified portion that is not Releasable cannot be REL at the resource level.
    </sch:p>
    <sch:p id="codeDesc">
        If CAPCO rules do not apply to the document then the rule does not apply
        and we return true. 
        
        Otherwise, we count the number of classified portions by looping through 
        $partTags. Then we get a list of all country codes listed in the releasableTo 
        attributes on those classified portions and tokenize them down so each node 
        of the list is a single token. If any of these tokens contains one of the 
        EYES countries designations (TEYE, ACGU, or FVEY) the countries that are 
        included in the designations are inserted. Then common countries are found 
        for the releasableTo tokens by finding the tokens whose count equals the 
        number of classified portions. The releasableTo values in the banner are 
        then compared to the final releasableTo common countries that were 
        determined based on the portion values.   
        
        If the attribute disseminationControls appears on the banner with a value 
        containing [REL] then the rule will return true if there are any [REL] portions 
        present and the banner values match the common countries between portions. 
        
        If the attribute disseminationControls appears on the banner with a value 
        containing [EYES] then the rule will return true if there are no [REL] portions 
        present and the banner values match the common countries between portions. 
        
        If the attribute disseminationControls does not appear on the banner or does 
        not contain the values [REL] or [EYES], then the rule will return true if 
        there are no common countries between portions.
    </sch:p>
    <sch:rule context="//*[generate-id(.) = generate-id($ISM_RESOURCE_ELEMENT)]">
        <sch:let name="numClassParts" value="
            count(
                for $each in $partTags return
                    if(matches($each/@ism:classification,'^(TS|S|C)$'))
                        then 1
                    else null
            )
            "/>
        <sch:let name="hasRel" value="
            count(
                for $token in $partTags return 
                    if(matches($token/@ism:classification, '^U$')) then null
                    else
                    if(index-of(tokenize($token/@ism:disseminationControls,' '), 'REL')>0)
                        then 1 
                    else null
            ) > 0
            "/>
        <sch:let name="codes" value="
            for $each in $partTags return
                if(matches($each/@ism:classification,'^(TS|S|C)$'))
                    then distinct-values(tokenize(normalize-space($each/@ism:releasableTo),' '))
                else null
            "/>
        <sch:let name="code_tok" value="for $each in $codes return tokenize($each,' ')"/>
        
        <!-- Expand THREE-, FOUR- and FIVE-EYES countries in the banner and portions -->
        <sch:let name="expandBannerEYES_tok" value="
            distinct-values(
                for $each in $bannerReleasableTo_tok return
                    if ($each = 'TEYE') then $TEYE_tok
                    else if ($each = 'ACGU') then $ACGU_tok
                    else if ($each = 'FVEY') then $FVEY_tok
                    else $each   
            )
            "/>
        <sch:let name="expandPortionEYES_tok" value="
            for $each in $code_tok return
                if ($each = 'TEYE') then $TEYE_tok
                else if ($each = 'ACGU') then $ACGU_tok
                else if ($each = 'FVEY') then $FVEY_tok
                else $each
            "/>
        
        <!-- Find countries common to displayOnlyTo and releasableTo portions -->
        <sch:let name="commonCountries" value="
            if (count($expandPortionEYES_tok)=0) then null
            else
                for $each in distinct-values($expandPortionEYES_tok) return
                    if (
                        count(
                            for $tok in $expandPortionEYES_tok return
                                if($each=$tok) then $tok else null
                        ) >= $numClassParts
                    ) then $each
                    else null                    
            "/>
        <sch:let name="finalRelTo" value="
            if (count($commonCountries) = 1 and $commonCountries = 'USA') then null
            else $commonCountries
            "/>
        
        <sch:let name="commonMatchesBanner" value="
            count($expandBannerEYES_tok) > 0 and 
            count($finalRelTo) > 0 and
            ( 
                every $tok in $expandBannerEYES_tok satisfies
                    index-of($finalRelTo, $tok) > 0
            ) and ( 
                every $tok in $finalRelTo satisfies
                    index-of($expandBannerEYES_tok, $tok) > 0
            )
            "/>
        
        <sch:assert 
            id="ISM-00088" 
            test="
            if(not($ISM_CAPCO_RESOURCE)) then true() 
            else if (index-of($bannerDisseminationControls_tok, 'REL')>0) then $hasRel and $commonMatchesBanner
            else if (index-of($bannerDisseminationControls_tok, 'EYES')>0) then not($hasRel) and $commonMatchesBanner
            else not(count($finalRelTo) > 0)
            " 
            flag="error">
            [ISM-ID-00088][Error] USA documents having any classified portion that is not Releasable cannot be REL at the resource level.
        </sch:assert>
    </sch:rule>
</sch:pattern>