Page 1 of 1

Validating dates in schematron

Posted: Tue Jun 20, 2017 12:36 pm
by Iwicka
Hi,

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

Valid XML:

Code: Select all

<dateA>2011-05-11</dateA>
<dateB>2011-05-10</dateB>
Not valid XML:

Code: Select all

<dateA>2011-05-11</dateA>
<dateB>2011-05-11</dateB>
I created the following rule comparing the two dates:

Code: Select all

<sch:rule context="//dateB">
<sch:assert test="(.) < dateA">ERROR</sch:assert>
</sch:rule>
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,

Ewa

Re: Validating dates in schematron

Posted: Tue Jun 20, 2017 3:24 pm
by tavy
Hello,

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="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2"
xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
<sch:pattern>
<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:description>
<sqf:title>Set current date to '<sch:value-of select="$expectedDate"/>'</sqf:title>
</sqf:description>
<sqf:replace match="text()" select="$expectedDate">
</sqf:replace>
</sqf:fix>
</sch:rule>
</sch:pattern>
</sch:schema>
Best Regards,
Octavian