@@ -473,28 +473,45 @@ private SchematronOutputType applySchematron(ISchematronResource schematron) thr
473473 private TAR validateSchematron (File schematronFile , boolean supportPureValidationApproach ) {
474474 SchematronOutputType svrlOutput ;
475475 boolean convertXPathExpressions = false ;
476+ boolean retryAsXslt = false ;
476477 String schematronFileName = schematronFile .getName ().toLowerCase ();
478+ ISchematronResource schematronResource = null ;
477479 try {
478480 if (schematronFileName .endsWith ("xslt" ) || schematronFileName .endsWith ("xsl" )) {
479481 // Validate as XSLT.
480- svrlOutput = applySchematron ( schematronAsXSLT (schematronFile ) );
482+ schematronResource = schematronAsXSLT (schematronFile );
481483 } else if (schematronFileName .endsWith ("sch" )) {
482484 // Validate as raw schematron.
483485 convertXPathExpressions = supportPureValidationApproach ;
484- svrlOutput = applySchematron ( schematronAsRaw (schematronFile , supportPureValidationApproach ) );
486+ schematronResource = schematronAsRaw (schematronFile , supportPureValidationApproach );
485487 } else {
486488 // We're not certain - validate as raw and if that fails validate as XSLT.
487- try {
488- convertXPathExpressions = supportPureValidationApproach ;
489- svrlOutput = applySchematron (schematronAsRaw (schematronFile , supportPureValidationApproach ));
490- } catch (Exception e ) {
489+ convertXPathExpressions = supportPureValidationApproach ;
490+ schematronResource = schematronAsRaw (schematronFile , supportPureValidationApproach );
491+ retryAsXslt = true ;
492+ }
493+ try {
494+ if (schematronResource instanceof SchematronResourcePure pureSchematron ) {
495+ pureSchematron .setErrorHandler (new PureSchematronErrorHandler ());
496+ }
497+ svrlOutput = applySchematron (schematronResource );
498+ } catch (Exception e ) {
499+ if (retryAsXslt ) {
491500 // Try also as XSLT.
492501 convertXPathExpressions = false ;
493502 svrlOutput = applySchematron (schematronAsXSLT (schematronFile ));
503+ } else {
504+ throw e ;
494505 }
495506 }
496507 } catch (Exception e ) {
497- throw new IllegalStateException ("Schematron file [" +schematronFile .getName ()+"] is invalid" , e );
508+ if (schematronResource instanceof SchematronResourcePure pureSchematron
509+ && pureSchematron .getErrorHandler () instanceof PureSchematronErrorHandler errorHandler
510+ && errorHandler .isDueToExternalFunctionCall ()) {
511+ throw new IllegalStateException ("Schematron file [" +schematronFile .getName ()+"] is provided in pure Schematron format (as a .sch file) and contains references to functions (built-in or external). To be able to use functions you must convert the Schematron file to its XSLT representation and use the XSLT file instead" , e );
512+ } else {
513+ throw new IllegalStateException ("Schematron file [" +schematronFile .getName ()+"] is invalid" , e );
514+ }
498515 }
499516 SchematronReportHandler handler = new SchematronReportHandler (specs .inputAsDocumentForSchematronValidation (), svrlOutput , convertXPathExpressions , specs .getDomainConfig ().isIncludeTestDefinition (), specs .getDomainConfig ().isIncludeAssertionID (), specs .isLocationAsPath (), specs .isShowLocationPaths (), specs .getLocalisationHelper ());
500517 return handler .createReport ();
0 commit comments