Loading TOC...

schematron:compile

schematron:compile(
   $schema as element(sch:schema),
   [$params as map:map?]
) as document-node()

Summary

This function compiles the Schematron schema. The Schematron schema is preprocessed and converted into the validator XSLT and returned. This function performs the first three stages of ISO Schematron. It accepts the Schematron root element and returns the document node of the XSLT validator script. This function will also optionally accept a map of parameters. The string key represents the parameter and each entry represents the value of the parameter.

Parameters
schema The Schematron root element.
params An optional map of parameters. The key value pairs acceptable for the params parameter are:
phase
A string representing the id of a phase to be treated as the active phase, or #ALL to activate all phases. #ALL is the default value. If this parameter is not specified, the phase stated by the defaultPhase attribute in the schema element is used as the active phase. If the defaultPhase attribute is not set, #ALL is used as the active phase
diagnose
If true (the default), diagnostics are added to the assertion test in reports. If false, diagnostics are not added.
allow-foreign
If true, enable passing non-Schematron elements and rich markup to the generated validator stylesheet. If false (the default), disable passing non-Schematron elements.
generate-fired-rule
If true (the default), generate fired-rule elements in the report to display the context. If false, do not generate fired-rule elements.
generate-paths
If true (the default), generate the @location attribute with XPaths in the assertion test in reports. If false, do not generate the @location attribute.
terminate
If true, terminate on the first failed assertion or successful report and throw XSLT-MSGTERMINATE. If false (the default), do not terminate.
validate-schema
If true, validate the Schematron schema. If the Schematron schema is bad, an XDMP-VALIDATEERRORS error is thrown. If false (the default), do not validate.

Usage Notes

The possible errors are as follows:
Possible Errors
SCHEMATRON-SCHEMANOTFOUND No document exists in the Schema database with the document URI specified.
SCHEMATRON-VALIDATORNOTFOUND The Schematron is not compiled and stored.
SCHEMATRON-NAMESPACE Schematron schema uses both old and new namespaces.
SCHEMATRON-QRYBINDING Invalid value for queryBinding attribute is specified.
SCHEMATRON-PHASE No phase has been defined with given the name.
SCHEMATRON-ACTIVE-NOPATTERN The active element has no pattern attribute.
SCHEMATRON-PATTERNREFERENCE Pattern declaration missing.
SCHEMATRON-ASSERT-NOTEST The assert element has no test attribute.
SCHEMATRON-REPORT-NOTEST The report element has no test attribute.
SCHEMATRON-DIAGNOSTIC-NOID The diagnostic element has no id attribute.
SCHEMATRON-EXTENDS-NORULE Declaration of abstract rule missing.
SCHEMATRON-RULEREFERENCE Abstract rule declaration missing.
SCHEMATRON-KEY-NONAME The key element has no name attribute.
SCHEMATRON-KEY-NOPATHORUSE The key element has no path or use attribute.
SCHEMATRON-KEY The key element is in the ISO Schematron namespace.
SCHEMATRON-FUNCTION-NONAME The function element has no name attribute.
SCHEMATRON-FUNCTION The function element in the ISO Schematron namespace.
SCHEMATRON-INCLUDEEMPTY Empty value for the href attribute in the include directive.
SCHEMATRON-INCLUDEBADURL Include directive contains a bad URL.
SCHEMATRON-INCLUDEBAD Unable to include the document referenced by include directive
SCHEMATRON-INCLUDE Include directive is used to reference the whole schema.
SCHEMATRON-CANNOTIMPORTSCHEMA Element import-schema is used for a query language binding that is not 'xslt2'
SCHEMATRON-IMPORTSCHEMA The import-schema element is in the ISO Schematron namespace.
SCHEMATRON-VARIABLES Incorrect use of variables.
SCHEMATRON-NS-NOURI The ns element has no uri attribute.
SCHEMATRON-NS-NOPREFIX The ns element has no prefix attribute.
SCHEMATRON-ABSTRACTPATTERNS Abstract patterns have not been pre-processed.
SCHEMATRON-PHASE-NOID The phase element has no id attribute.
SCHEMATRON-RULE-NOCONTEXT The rule element has no context attribute.
SCHEMATRON-ABSTRACTRULE-NOID The abstract rule has no id attribute.
SCHEMATRON-ABSTRACTRULE-CONTEXT The abstract rule has has context attribute.
SCHEMATRON-VALUEOF-NOSELECT The value element has no select attribute.
SCHEMATRON-HASCHILD Child element is unexpected for the given element.
SCHEMATRON-DIAGNOSTICREFERENCE Diagnostic declaration missing.
SCHEMATRON-UNKNOWN-SCHMTRNELEMENT Unknown Schematorn element is present.
SCHEMATRON-UNKNOWNELEMENT Unknown element is present.
SCHEMATRON-BAD Bad Schema.

Example

(: Note that the queryBinding="xslt2" attribute in the schema file directs Schematron to make 
   use of the xslt 2.0 engine. :)

xquery version "1.0-ml"; 
 
import module namespace schematron = "http://marklogic.com/xdmp/schematron" 
      at "/MarkLogic/schematron/schematron.xqy";

let $schema :=
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2" schemaVersion="1.0">
<sch:title>user-validation</sch:title>
<sch:phase id="phase1">
  <sch:active pattern="structural"></sch:active>
</sch:phase>
<sch:phase id="phase2">
  <sch:active pattern="co-occurence"></sch:active>
</sch:phase>
<sch:pattern id="structural">
  <sch:rule context="user">
    <sch:assert test="@id">user element should have an id attribute</sch:assert>
    <sch:assert test="count(*) = 5">
        user element should have 5 child elements: name, gender, age, score and result
    </sch:assert>
    <sch:assert test="score/@total">score element should have a total attribute</sch:assert>
    <sch:assert test="score/count(*) = 2">score element should have two child elements</sch:assert>
  </sch:rule>
</sch:pattern>
<sch:pattern id="co-occurence">
  <sch:rule context="score">
    <sch:assert test="@total = test-1 + test-2">total score must be a sum of test-1 
                                                and test-2 scores</sch:assert>
    <sch:assert test="(@total gt 30 and ../result = 'pass') or 
                      (@total le 30 and ../result = 'fail')" diagnostics="d1">
               if the score is greater than 30 then the result should be 'pass' else 'fail'
    </sch:assert>
  </sch:rule>
</sch:pattern>
<sch:diagnostics>
<sch:diagnostic id="d1">the score does not match with the result</sch:diagnostic>
</sch:diagnostics>
</sch:schema>

let $params := map:map()
let $_put := map:put($params, 'phase', '#ALL')
let $_put := map:put($params, 'terminate', fn:false())
let $_put := map:put($params, 'generate-fired-rule', fn:true())
let $_put := map:put($params, 'generate-paths', fn:true())
let $_put := map:put($params, 'diagnose', fn:true())
let $_put := map:put($params, 'allow-foreign', fn:false())
let $_put := map:put($params, 'validate-schema', fn:true())

return schematron:compile($schema, $params) 

==>

returns the compiled Schematron schema (validator XSLT)     
 
    

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.