<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:sqf="http://www.schematron-quickfix.com/validator/process"
    queryBinding="xslt2">
    <ns prefix="deltaxml" uri="http://www.deltaxml.com/ns/well-formed-delta-v1"/>
    <ns prefix="saxon" uri="http://saxon.sf.net/"/>

    <!-- include patterns from deltaV2 -->
    <include href="delta-common.sch#COMMON-root-rules"/>
    <include href="delta-common.sch#COMMON-subtree-delta-rules"/>
    <include href="delta-common.sch#COMMON-text-rules"/>
    
    <!-- include patterns from deltaV2 merge-->
    <include href="deltaV2-merge.sch#all-format-merge-root-checks"/>
    <include href="deltaV2-merge.sch#all-format-merge-delta-value-rules"/>
    <include href="deltaV2-merge.sch#V2-merge-delta-value-rules"/>
    
    <xsl:include href="xsl/delta-2-functions.xsl"/>
    <xsl:include href="xsl/correct-with-order.xsl"/>
    
    <xsl:variable name="delta-version-order" select="tokenize(/*/@deltaxml:version-order, ', ')" as="xs:string*" />
    
    
    <pattern id="V2-sequential-merge-root-checks">
        <rule id="V2-content-type" context="/*">
            <assert id="V2-content-type-value"
                test="@deltaxml:content-type = ('merge-sequential')">There must be a deltaxml:content-type attribute with the value 'merge-sequential'</assert>
        </rule>
    </pattern>
    
    <pattern id="V2-sequential-merge-delta-value-rules">
        <rule id="delta-versions" context="*[@deltaxml:deltaV2]">
            <assert id="version-missing-in-between" test="count(tokenize(@deltaxml:deltaV2,'!=|=')) ge
                (
                deltaxml:getVersionOrdinal(tokenize(@deltaxml:deltaV2,'!=|=')[last()], $delta-version-order) 
                - deltaxml:getVersionOrdinal(tokenize(@deltaxml:deltaV2,'!=|=')[1], $delta-version-order)
                + 1
                )">
                A version is missing in between the version order '<value-of select="tokenize(@deltaxml:deltaV2,'!=|=')"/>' in the deltaxml:deltaV2 
                '<value-of select="@deltaxml:deltaV2"/>' for the element at path <value-of select="saxon:path(.)"/>
            </assert>
            <assert id="order-of-versions" test="deltaxml:areVersionsInOrder(tokenize(@deltaxml:deltaV2,'!=|=') ,$delta-version-order)">
                The versions '<value-of select="tokenize(@deltaxml:deltaV2,'!=|=')"/>' in the deltaxml:deltaV2 '<value-of select="@deltaxml:deltaV2"/>' 
                are in wrong order for the element at path <value-of select="saxon:path(.)"/>
            </assert>
        </rule>
    </pattern>
    
    <pattern id="V2-sequential-merge-element-rules">
       <include href="deltaV2-merge.sch#common-merge-type-element-rules"/>
        
       <rule context="*/*[@deltaxml:deltaV2]" id="sequential-inconsistent-child-versions">
           <assert test="contains(parent::*/@deltaxml:deltaV2, @deltaxml:deltaV2)"
               >The element at <value-of select="saxon:path(.)"/> has a deltaV2 value that is inconsistent with its parent.</assert>        
       </rule>
    </pattern>
    
    <pattern id="V2-sequential-merge-attribute-rules">
        <include href="delta-common.sch#COMMON-deltaxml-attributes"/>
        <include href="delta-common.sch#COMMON-deltaxml-attributes-child"/>
        <rule id="sequential-merge-attributeValue" context="deltaxml:attributeValue">
            <include href="delta-common.sch#COMMON-att-value-ancestry"/>
            <include href="delta-common.sch#COMMON-att-value-content-type"/>
            <include href="delta-common.sch#COMMON-att-value-content"/>
            <include href="delta-common.sch#COMMON-att-value-delta-inequality"/>
            <include href="delta-common.sch#COMMON-att-value-delta-uniqueness"/>
            <include href="delta-common.sch#COMMON-att-value-single-entry-per-version"/>
        </rule>
    </pattern>
</schema>