Sample Code

Some code samples

Schematron sample code

This article contains some general Schematron code samples to enable a quick start when writing Schematron validation code in PageSeeder.

For more specific code samples, see:

Basic Schematron

The sample code below is the basic structure of Schematron schema with some placeholders for personal code.

<?xml version="1.0"?>
<!--
  A sample schematron
-->
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron"
            title="Rules for [type] documents">

  <!-- You can creates as many patterns as you like -->
  <sch:pattern name="[your name]">

    <!-- Rules matchings an element in your document -->
    <sch:rule context="[your XSLT pattern]">

      <!-- Reported as an info if test is true -->
      <sch:report test="[your xpath test]">[your message]</sch:report>

      <!-- Failed assertions are reported as errors -->
      <sch:assert test="[your xpath]">[your message]</sch:assert>

      <!-- More <sch:report> or <sch:assert> ... -->

    </sch:rule>
    <!-- More <sch:rule> ... -->

  </sch:pattern>
  <!-- More <sch:pattern> ... -->

</sch:schema>

Example

<?xml version="1.0"?>
<!--
  This schematron validates a PSML document.

  The schematron rules can be used to enforce additional constraints required
  by the application.

  @see https://dev.pageseeder.com/api/data/psml.html
-->
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron"
            title="Rules for tutorial documents">

  <!--
    Set of rules applying to the entire document
  -->
  <sch:pattern name="Document">

    <sch:rule context="/document">

      <!-- Too many sections -->
      <sch:assert test="count(section) le 20">Your document contains over 20
      sections. You should consider splitting your document into multiple
      documents.</sch:assert>

      <!-- No Doc ID -->
      <sch:report test="not(documentinfo/uri/@docid)">Your document
      does not have a Doc ID.</sch:report>

      <!-- No Label -->
      <sch:report test="not(documentinfo/uri/@labels)">Your document
      has no label.</sch:report>

    </sch:rule>

  </sch:pattern>

  <!--
    Set of rules applying to the document fragments
  -->
  <sch:pattern name="Fragments">

    <sch:rule context="section/fragment">

      <!-- Fragment has no heading -->
      <sch:assert test="name(*[1]) = 'heading'">Fragment
      '<sch:value-of select="@id"/>' has no title.</sch:assert>

      <!-- Fragment is too long (over 2000 chars)-->
      <sch:assert test="string-length(string-join(.//text(), '')) le 2000">
      Fragment '<sch:value-of select="@id"/>' has over 2000 characters.
      </sch:assert>

    </sch:rule>

  </sch:pattern>

</sch:schema>

Rule to check cross referenced document content

This rule checks the cross referenced document content.

<!-- Match cross references -->
<sch:rule context="xref|blockxref">

  <!-- Check referenced document contains a heading2 -->
  <sch:assert test="document(@href)//heading2">Document
    <sch:value-of select="@href"/> has no heading2.</sch:assert>

 </sch:rule>

Created on , last edited on