5
votes

I'm trying to add schematron validation to my xsd. This is my new xsd :

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    xmlns:sch="http://www.ascc.net/xml/schematron"    
    elementFormDefault="qualified" &gt;

 <xs:element name="books"> 
  <xs:complextype>
   <xs:sequence>   ;P 
    <xs:element name="book" type="bookType" maxoccurs="unbounded">
      <xs:annotation>
       <xs:appinfo>
        <sch:pattern id="onLoanTests" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
          <sch:rule context="book">
           <sch:report test="@on-loan and not(@return-date)">
           Every book that is on loan must have a return date
           </sch:report>
          </sch:rule>
        </sch:pattern>
       </xs:appinfo>
      </xs:annotation>
    </xs:element>
   </xs:sequence> 
  </xs:complextype>
 </xs:element>

 <xs:complextype name="bookType">
  <xs:sequence>
   <xs:element name="title" type="xs:string" />
   <xs:element name="author" type="xs:string" />
   <xs:element name="publication-date" type="xs:string" />
  </xs:sequence>
  <xs:attribute name="publisher" type="xs:string" use="required" />
  <xs:attribute name="on-loan" type="xs:string" use="required" />
  <xs:attribute name="return-date" type="xs:string" use="optional" />
 </xs:complextype>

</xs:schema>

This is my test xml :

<books>
<book publisher="ddd" on-loan="sdsd">
  <title>idan title</title> 
  <author>idan author</author> 
  <publication-date>idan date</publication-date> 
</book>
</books>

Using the xml I provided I don't get validation error.

I assumed I will get the message "Every book that is on loan must have a return date" And that the xml won't be valid. Suggestions as to why ?

Update I did manage to make it work by using the schematron validation in oXygen xml editor. However, how am I suppose to use in my code ? Do I need to install something special ? link to another library ?

Update2 Apparently here in the "Processing" section, all the needed steps are detailed.

1

1 Answers

8
votes

Your second update is probably the best reference to the subject. XSD itself does not allow you a mechanism for validating against a schematron as well as the schema itself. The xsd:appinfo element does allow you to be embed validation information for a different schema language but it is specifically for application use (hence the name).

That means you need to do something to enable it. The paper you referenced gives the best approach which boils down to:

  1. Use XSLT to extract the schematron rules from your XSD
  2. Use the reference XSLT implementation from schematron.com to convert the schema to XSLT
  3. Validate your instance document against the XSD
  4. Validate your instance document against the schematron by processing the XSLT created in 2.

Depending on your environment you might want to consider looking at an XProc implementation (calabash or calumet) to achieve that pipeline.