How to compare dates using XML and Schematron -


schematron xml date comparison in not working. , returns false error. think using schematron 1.01. using c# , dll msdn: msdn schematron link

here error output (by way passes xsd validation):

from pattern "check co-occurrence constraints"     assert fails: visitdate must within valid date range specified in databegindate , dataenddate     at: /mydata[1]/site[1]/patient[1]/form_baseline[1]/dat_visitdate[1]         <dat_visitdate signedwho="system" value="2009-02-02">...</dat_visitdate>         (line: 7, column: 10) 

schematron rule:

<sch:rule context="mydata/site/patient/form_baseline/dat_visitdate">   <sch:assert test="@value > //mydata/@databegindate">visitdate must within valid date range specified in databegindate , dataenddate</sch:assert> </sch:rule> 

xml:

<?xml version="1.0" encoding="utf-8"?> <mydata xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" databegindate="2008-01-01" dataenddate="2011-04-26">   <site sitekey="11">     <patient patientkey="33">       <dat_statusdate signedwho="system" value="2009-01-01" />       <form_baseline>         <dat_visitdate signedwho="system" value="2009-02-02" />       </form_baseline>       <form_flowsheet visitdatekey="2009-03-03">         <dat_otherdate signedwho="system" value="2009-03-03" />       </form_flowsheet>     </patient>   </site> </mydata> 

xsd:

<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"    xmlns:sch="http://www.ascc.net/xml/schematron">    <xs:annotation>     <xs:appinfo>       <sch:title>schematron validation</sch:title>     </xs:appinfo>   </xs:annotation>    <xs:simpletype name="datemydate">     <xs:restriction base="xs:date">       <xs:mininclusive value="2000-01-01"/>     </xs:restriction>   </xs:simpletype>          <xs:simpletype name="integermykey">     <xs:restriction base="xs:integer">       <xs:mininclusive value="1"/>       <xs:maxinclusive value="9999"/>     </xs:restriction>   </xs:simpletype>      <xs:simpletype name="stringnonempty">     <xs:restriction base="xs:string">       <xs:minlength value="1"/>       <xs:maxlength value="32"/>     </xs:restriction>   </xs:simpletype>    <xs:element name="mydata">     <xs:complextype>       <xs:sequence>         <xs:element name="site" minoccurs="1" maxoccurs="1">           <xs:complextype>             <xs:sequence>               <xs:element name="patient" minoccurs="0" maxoccurs="unbounded">                 <xs:complextype>                   <xs:sequence>                      <xs:element name="dat_statusdate" minoccurs="1" maxoccurs="1">                       <xs:complextype>                         <xs:attribute name="value" type="datemydate" use="required" />                         <xs:attribute name="signedwho" type="stringnonempty" use="required" />                       </xs:complextype>                     </xs:element>                     <xs:element name="form_baseline" minoccurs="0" maxoccurs="1">                       <xs:complextype>                         <xs:sequence>                           <xs:element name="dat_visitdate" minoccurs="1" maxoccurs="1">                             <xs:complextype>                   <xs:annotation>                 <xs:appinfo>                   <sch:pattern name="check co-occurrence constraints">                   <sch:rule context="mydata/site/patient/form_baseline/dat_visitdate">                     <sch:assert test="@value > //mydata/@databegindate">                        visitdate must within valid date range specified in databegindate , dataenddate                     </sch:assert>                   </sch:rule>                   </sch:pattern>                  </xs:appinfo>                 </xs:annotation>                                                                 <xs:attribute name="value" type="datemydate" use="required" />                               <xs:attribute name="signedwho" type="stringnonempty" use="required" />                             </xs:complextype>                           </xs:element>                          </xs:sequence>                       </xs:complextype>                     </xs:element>                                                <xs:element name="form_flowsheet" minoccurs="0" maxoccurs="unbounded">                       <xs:complextype>                         <xs:sequence>                           <xs:element name="dat_otherdate" minoccurs="1" maxoccurs="1">                             <xs:complextype>                               <xs:attribute name="value" type="datemydate" use="required" />                               <xs:attribute name="signedwho" type="stringnonempty" use="required" />                             </xs:complextype>                           </xs:element>                         </xs:sequence>                       <xs:attribute name="visitdatekey" type="datemydate" use="required" />                       </xs:complextype>                     </xs:element>                    </xs:sequence>                   <xs:attribute name="patientkey" type="stringnonempty" use="required" />                 </xs:complextype>               </xs:element>             </xs:sequence>            <xs:attribute name="sitekey" type="integermykey" use="required" />            </xs:complextype>         </xs:element>       </xs:sequence>       <xs:attribute name="databegindate" type="datemydate" use="required" />       <xs:attribute name="dataenddate" type="datemydate" use="required" />     </xs:complextype>   </xs:element> </xs:schema> 

found answer:

xpath 1.0 uses comparison operators < , > numbers. must convert dates number value.

<sch:assert test="translate(@value, '-', '') > translate(//mydata/@databegindate, '-', '')"> 

or more explicitly:

<sch:assert test="number(translate(@value, '-', '')) > number(translate(//mydata/@databegindate, '-', ''))"> 

Comments