1
votes

How do I validate that all 'signedWhen' xml attributes should not have time zone information using Schematron? I am using the .NET implementation that uses XPath 1.0.

Given source XML:

<?xml version="1.0" encoding="utf-8"?>
<MyData versionDate="2010-12-09" dataBeginDate="2012-03-01" dataEndDate="2012-03-10" extractedWhen="2012-03-09T10:08:40">
  <Site Site_key="999">
    <SitePatient Patient_key="1">
    <txt_Surname value="TEST" signedWhen="2012-03-08T22:02:39" signedWho="SomeName"/>
    <txt_GivenNames value="PATIENT" signedWhen="2012-03-08T22:02:39" signedWho="SomeName"/>
    <dat_BirthDate value="2010-06-15" signedWhen="2012-03-08T22:02:39" signedWho="SomeName"/>
    <sel_Status value="Enrolled" signedWhen="2012-03-08T22:02:39" signedWho="SomeName"/>
    <dat_StatusDate value="2012-03-05-05:00" signedWhen="2012-03-08T22:02:39" signedWho="SomeName"/>
    </SitePatient>
  </Site>
</MyData>

Using this Schematron rule in the XSD file:

<xs:annotation>
<xs:appinfo>
  <sch:pattern name="All signedWhen TimeZone constraints">
  <sch:rule context="*[@signedWhen]">
  <sch:assert test="(substring(@signedWhen,11,12) != '-') and (substring(@signedWhen,11,12) != '+') and (substring(@signedWhen,11,12) != 'Z')">
       <name/> must not include TimeZone information
    </sch:assert>
  </sch:rule>
  </sch:pattern> 
</xs:appinfo>  
</xs:annotation>

Gives these incorrect results

It should not return any results as there is no time zone information.

NMatrix.Schematron.ValidationException: Results from Schematron validation:
Results from Schematron validation
From pattern "All signedWhen TimeZone constraints"
Assert fails: txt_Surname must not include TimeZone information
At: /MyData[1]/Site[1]/SitePatient[1]/txt_Surname[1]
    <txt_Surname value="TEST" signedWhen="2012-03-08T22:02:39" signedWho="SomeName">...</txt_Surname>
    (Line: 5, Column: 6)
Assert fails: txt_GivenNames must not include TimeZone information
At: /MyData[1]/Site[1]/SitePatient[1]/txt_GivenNames[1]
    <txt_GivenNames value="PATIENT" signedWhen="2012-03-08T22:02:39" signedWho="SomeName">...</txt_GivenNames>
    (Line: 6, Column: 6)
Assert fails: dat_BirthDate must not include TimeZone information
At: /MyData[1]/Site[1]/SitePatient[1]/dat_BirthDate[1]
    <dat_BirthDate value="2010-06-15" signedWhen="2012-03-08T22:02:39" signedWho="SomeName">...</dat_BirthDate>
    (Line: 7, Column: 6)
Assert fails: sel_Status must not include TimeZone information
At: /MyData[1]/Site[1]/SitePatient[1]/sel_Status[1]
    <sel_Status value="Enrolled" signedWhen="2012-03-08T22:02:39" signedWho="SomeName">...</sel_Status>
    (Line: 8, Column: 6)
Assert fails: dat_StatusDate must not include TimeZone information
At: /MyData[1]/Site[1]/SitePatient[1]/dat_StatusDate[1]
    <dat_StatusDate value="2012-03-05-05:00" signedWhen="2012-03-08T22:02:39" signedWho="SomeName">...</dat_StatusDate>
    (Line: 9, Column: 6)

EDIT 1:

I figured it out. I was not using the XPath 1.0 function "substring" correctly in my tests.

<sch:assert test="(substring(@signedWhen, 11, 1) != '-') and (substring(@signedWhen, 11, 1) != '+') and (substring(@signedWhen, 11, 1) != 'Z')">

EDIT 2: W3schools defintion of xs:date and xs:dateTime did not mention the possibility of the optionally negative signed year. So my code above would not work, see the answer selected below.

EDIT 3: Well the schema I am using (xmlns:xs="http://www.w3.org/2001/XMLSchema" ) does not accept a negative signed year - and declares it invalid. But to be safe I will use this code from now on:

<sch:assert test="not ( (contains(substring(@signedWhen, 11, 2), '-')) or (contains(@signedWhen, '+')) or (contains(@signedWhen, 'Z')) )">
1

1 Answers

1
votes

I don't know if you want to handle date or datetime, but when I see your code, maybe both. Your code won't work in some specific cases.

Given the dateTime specification :

The ·lexical space· of dateTime consists of finite-length sequences of characters of the form: '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?, where ...

You can read further for details. The (zzz)? part is the timezone and is as follow (same source) :

The lexical representation of a timezone is a string of the form: (('+' | '-') hh ':' mm) | 'Z', where...

So your code won't work for date or dateTime like : -2010-03-08Z or 2010-03-08T12:01:02.2Z, for example.

This one should be more generic (and XPath 1.0) :

test="not(
            contains(@signedWhen,'+') 
            or contains(@signedWhen,'Z') 
            or (contains(@signedWhen, 'T') 
                 and contains(substring-after(@signedWhen ,':'),'-'))
            or (not(contains(@signedWhen,'T')) and contains(@signedWhen,':'))
            )"