Edit online

Schematron Unit Test (XSpec)

XSpec is a behavior driven development (BDD) framework for XSLT, XQuery, and Schematron. XSpec consists of syntax for describing the behavior of your XSLT, XQuery, or Schematron code, and some code that enables you to test your code against those descriptions.

Creating a Schematron Unit Test

To create a Schematron Unit Test, go to File > New > Schematron Unit Test. This is simple document template to help you get started.

Running a Schematron Unit Test

To run a Unit Test, open the XSpec file in an editor and click Apply Transformation Scenario(s) on the main toolbar. This will run the built-in Run XSpec Test transformation scenario that is defined in the XSpec framework.

Testing a Stylesheet

An XSpec file contains one or more test scenarios.

Example

Suppose you have this Schematron rule that says sections should have a title:
  <sch:pattern>
    <sch:rule context="section">
      <sch:assert test="title" id="a002">
        section should have a title
      </sch:assert>
    </sch:rule>
  </sch:pattern>
The XSpec test could look like this:
<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec" schematron="demo-01.sch">
    <x:scenario label="section should have a title">
      <x:context>
        <article>
          <section>
            <title>Introduction</title>
            <p>This is an example.</p>
          </section>
          <section>
            <p>This is an example.</p>
          </section>
        </article>
      </x:context>
      
      <x:expect-not-assert id="a002" location="/article[1]/section[1]"/>
      <x:expect-assert id="a002" location="/article[1]/section[2]"/>
    </x:scenario>
</x:description>

The <sch:assert> with the id="a002" is not expected to be triggered on the first section since it includes a title. This requirement is expressed with the <x:expect-not-assert> element.

Since the second section does not have a title, you would expect the Schematron rule to be triggered and this requirement is expressed with the <x:expect-assert> element.

For more details about how to write Schematron tests and various samples, see https://github.com/xspec/xspec/wiki/Writing-Scenarios-for-Schematron#writing-tests.

Adding a Catalog to an XSpec Transformation

If your Schematron needs a catalog, you can add one to the XSpec transformation by doing one of the following:
  • If you are using a project in Oxygen XML Editor, create a catalog.xml file in the project directory. This catalog will then be loaded automatically.
  • Edit the Run XSpec Test transformation scenario, go to the Parameters tab, and set the value of the catalog parameter to the location of your catalog file.