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 following is a boilerplate Schematron schema. It has placeholders for different types of validation rules.

<?xml version="1.0"?>
<!--   A sample 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]

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

            <!-- Add more <sch:report>
                         or <sch:assert>... -->

            <!-- Add more <sch:rule> ... -->

            <!-- Add more <sch:pattern> ... -->


Example Schema

The following schema is designed to validate some generic best practice rules for PSML.

<?xml version="1.0"?>

<!--  This schematron validates a PSML document.

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


         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">This
        document contains over 20 sections. Consider
        whether to split it into multiple documents.

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

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


          <!-- Validation rules for 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
test="string-length(string-join(.//text(), '')) le 2000">
      Fragment '<sch:value-of select="@id"/>'
                  has over 2000 characters.


Check cross referenced document

A rule to check the content of an XRef target document.

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

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


Created on , last edited on