Previous Up Next


p:validate-with-schematron

p:validate-with-schematron — Validates a document using a Schematron schema.

Synopsis

<p:declare-step type="p:validate-with-schematron">
     <p:input port="parameters" kind="parameter"/>
     <p:input port="source" primary="true"/>
     <p:input port="schema"/>
     <p:output port="result" primary="true"/>
     <p:output port="report" sequence="true"/>
     <p:option name="phase" select="'#ALL'"/>                      <!-- string -->
     <p:option name="assert-valid" select="'true'"/>               <!-- boolean -->
</p:declare-step>

Description

The p:validate-with-schematron step applies [Schematron] processing to its input document. If assert-valid is true, the step fails if any Schematron assertions fail, otherwise a copy of the input appears on the output.

The value of the phase option identifies the Schematron validation phase with which validation begins. The parameters port provides name/value pairs which correspond to Schematron external variables.

The result output from this step is a copy of the input. Schematron assertions and reports, if any, appear on the report port. The output should be in Schematron Validation Report Language (SVRL).

Errors

Error Description
err:C0054 Occurs if the assert-valid option is true and any Schematron assertions fail.

Examples

The Schematron language is (XPath) rule based, rather than grammar based (like XML Schema and RELAX NG). As a consequence, it can be used to test assertions of arbitrary complexity.

One such example is the DocBook rule about bibliography references. The DocBook biblioref element must point to a bibliography entry. Grammar-based parsers can check that the IDREF attribute on a biblioref element points to an ID that exists, but they don't check to see what kind of element has that ID.

Enter Schematron. Here's a simple rule that tests biblioref elements:

  1 <s:pattern name="'biblioref' type constraint">
       <s:rule context="db:biblioref[@linkend]">
          <s:assert test="local-name(//*[@xml:id=current()/@linkend]) = 'bibliomixed'
              and namespace-uri(//*[@xml:id=current()/@linkend])
  5               = 'http://docbook.org/ns/docbook'">@linkend on biblioref must point
    to a bibliography entry.</s:assert>
       </s:rule>
    </s:pattern>

This rule asserts that when a bibloref element has a linkend attribute, that attribute's value must be the xml:id value of a bibliomixed element in the DocBook namespace. (A slight simplification of the real rule which checks for a few other elements as well.)

  1 <p:pipeline xmlns:p="http://www.w3.org/ns/xproc" name="main"
                version="1.0">
    
      <p:validate-with-schematron>
  5     <p:input port="schema">
          <p:document href="../docs/rules.sch"/>
        </p:input>
      </p:validate-with-schematron>
    </p:pipeline>
Input Output
1 <chapter xmlns="http://docbook.org/ns/docbook">
<title>Chapter title</title>
<para>What about this book: <biblioref linkend="someid"/>.</para>
<bibliography>
5 <bibliomixed xml:id="someid"><abbrev>BOOK</abbrev> Title, Author,
etc.</bibliomixed>
</bibliography>
</chapter>
 
1 <chapter xmlns="http://docbook.org/ns/docbook">
<title>Chapter title</title>
<para>What about this book: <biblioref linkend="someid"/>.</para>
<bibliography>
5 <bibliomixed xml:id="someid"><abbrev>BOOK</abbrev> Title, Author,
etc.</bibliomixed>
</bibliography>
</chapter>

When an assertion fails, the error message is sent to the report port.

  1 <p:pipeline xmlns:p="http://www.w3.org/ns/xproc" name="main"
                version="1.0">
    
      <p:validate-with-schematron name="schematron"
  5                               assert-valid="false">
        <p:input port="schema">
          <p:document href="../docs/rules.sch"/>
        </p:input>
      </p:validate-with-schematron>
 10 
      <p:sink/>
    
      <p:identity>
        <p:input port="source">
 15       <p:pipe step="schematron" port="report"/>
        </p:input>
      </p:identity>
    </p:pipeline>
Input Output
1 <chapter xmlns="http://docbook.org/ns/docbook">
<title>Chapter title</title>
<para>What about this book: <biblioref linkend="bibl"/>.</para>
<bibliography xml:id="bibl">
5 <bibliomixed xml:id="someid"><abbrev>BOOK</abbrev> Title, Author,
etc.</bibliomixed>
</bibliography>
</chapter>
 
1 <svrl:schematron-output xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
xmlns:iso="http://purl.oclc.org/dsdl/schematron"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:schold="http://www.ascc.net/xml/schematron"
5 xmlns:db="http://docbook.org/ns/docbook"
title=""
schemaVersion="">
<!-->
<svrl:ns-prefix-in-attribute-values uri="http://docbook.org/ns/docbook"
10 prefix="db"/>
<svrl:active-pattern document="file:/projects/xproc/src/core/build/../xpl/validate-with-schematron-2.xpl"/>
<svrl:fired-rule context="db:biblioref[@linkend]"/>
<svrl:failed-assert test="local-name(//*[@xml:id=current()/@linkend]) = 'bibliomixed' and namespace-uri(//*[@xml:id=current()/@linkend]) = 'http://docbook.org/ns/docbook'"
location="/*[local-name()='chapter']/*[local-name()='para']/*[local-name()='biblioref'>
15 <svrl:text>@linkend on biblioref must point to a bibliography entry.</svrl:text>
</svrl:failed-assert>
</svrl:schematron-output>

If you want to read the report output, you must specify that assert-valid is “false”, otherwise the step fails and there's no way to read the report output.