Loading TOC...

MarkLogic 10 Product Documentation
schematron.compile

schematron.compile(
   schema as element(sch.schema),
   [params as Object?]
) as 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.

//  Note that line returns on XML schema files need to be escaped in javaScript.

const schematron = require("/MarkLogic/schematron/schematron.xqy");

const schema = fn.head(xdmp.unquote('<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" \
                                      queryBinding="xslt2" schemaVersion="1.0">\
<sch:title>request-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="request">\
    <sch:assert test="id">request should have an id</sch:assert>\
    <sch:assert test="count(*) = 7">request should have 7 properties: id, \
        description, maintainance, fixed, raisedBy, category and fixedBy \
    </sch:assert>\
  </sch:rule>\
</sch:pattern>\
<sch:pattern id="co-occurence">\
  <sch:rule context="request">\
    <sch:assert test="(category = \'doors\' and fixedBy = \'Tom\') or \
                      (category != \'doors\')" diagnostics="d1"> \
               if category is doors then fixedBy must be Tom \
    </sch:assert>\
  </sch:rule>\
</sch:pattern>\
<sch:diagnostics>\
<sch:diagnostic id="d1">Requests of doors category should be fixed by Tom</sch:diagnostic>\
</sch:diagnostics>\
</sch:schema>'))  

const params = 
{
  "phase" : "phase2",
  "terminate" : false,
  "generate-fired-rule" : true,
  "generate-paths" : true,
  "diagnose" : true,
  "allow-foreign" : false,
  "validate-schema" : true
}    

schematron.compile(schema.root, 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.