Validating dates in schematron

Having trouble installing <oXygen/>? Got a bug to report? Post it all here.
Posts: 11

Validating dates in schematron

Tue Jun 20, 2017 12:36 pm


I need to validate the following using schematron: date B must be at 1 day earlier than date A:

Valid XML:

Code: Select all


Not valid XML:

Code: Select all


I created the following rule comparing the two dates:

Code: Select all

<sch:rule context="//dateB">
            <sch:assert test="(.) &lt;  dateA">ERROR</sch:assert>

but I'm not sure how to say "one day earlier". Do I need to convert the date into number or is there a way to do it directly from the date type in XSLT2?

Thanks in advance,

Posts: 144

Re: Validating dates in schematron

Tue Jun 20, 2017 3:24 pm


You can convert to 'xs:date' and compare the dates. To obtain the dateA you need to get the previous sibling using the following XPath 'preceding-sibling::dateA[1]', and then decrease one day to obtain the expected date. You can provide also a fix that will correct the current date. The Schematron file can be something like this:

Code: Select all

<sch:schema xmlns:sch="" queryBinding="xslt2"
        <sch:rule context="dateB">
            <sch:let name="dateA" value="xs:date(preceding-sibling::dateA[1])"/>
            <sch:let name="expectedDate" value="$dateA - xs:dayTimeDuration('P1D')"/>
            <sch:assert test="xs:date(.) = $expectedDate" sqf:fix="setDateB">
                ERROR The current date '<sch:value-of select="."/>' should be one day earlier than date A '<sch:value-of select="$dateA"/>' </sch:assert>
            <sqf:fix id="setDateB">
                    <sqf:title>Set current date to '<sch:value-of select="$expectedDate"/>'</sqf:title>
                <sqf:replace match="text()" select="$expectedDate">

Best Regards,
Octavian Nadolu
<oXygen/> XML Editor

Return to “Common Problems”

Who is online

Users browsing this forum: Bing [Bot] and 2 guests