1
votes

How can I reuse a Schematron assert test using XPath 1.0? Specifically how can I reuse a test using the found attribute name. If I cannot do this I would like to reuse a test and just have one test for each named attribute. (I thought only XPath 2.0 can use variables). As you can see from the XSD schema, I have the same test but am using different attribute names. Note: I know that the test for timezone is not foolproof, it is just an example.

Given 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_MyName value="test myname"/>
      <txt_Surname value="test surname" signedWhen="2012-03-08T22:02:39"/>
      <dat_BirthDate value="2010-06-15" signedWhen="2012-03-08T22:02:39"/>
      <sel_Status value="Enrolled" signedWhen="2012-03-08T22:02:39"/>
      <dat_StatusDate value="2012-03-05-05:00" signedWhen="2012-03-08T22:02:39"/>
      <sel_Something value="" valueDate="2012-03-08" signedWhen="2012-03-08T22:02:39"/>
    </SitePatient>
  </Site>
</MyData>

Given XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:sch="http://www.ascc.net/xml/schematron">
<xs:annotation>
<xs:appinfo>
  <sch:title>Schematron Validation</sch:title>
</xs:appinfo>

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

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

1 Answers

1
votes

From your first given context, "*[@signedWhen], you should have access to the current node. You can pull the others attributes via "./@value". Additionally, you can hold onto this value using a sch:let as a variable. That's how you can relate both those data values, although I'm not entirely sure if that was your full question.