.NET中使用Saxon直接基于Schematron(.sch)验证XML文件
Great question! You’re right that most Saxon examples focus on straight XSLT usage, but you absolutely can validate your XML against a Schematron file directly—you just need to account for how Schematron works under the hood (it compiles to XSLT for execution). Here’s a step-by-step guide for both command-line and programmatic workflows:
1. Understand the Core Workflow
Schematron isn’t executed directly by Saxon; instead, it first gets transformed into a validation XSLT stylesheet (following the SVRL, Schematron Validation Report Language, standard). For this, you’ll need the official Schematron XSLT conversion scripts (these are part of the Schematron specification distribution).
2. Command-Line Approach (Works with All Saxon Versions)
Step 1: Convert Schematron to Validation XSLT
First, use Saxon to turn your .sch file into an XSLT that can validate your XML. Use the iso_svrl_for_xslt2.xsl script (ideal for Saxon, which supports XSLT 2.0+):
saxon -s:your-schema.sch -xsl:iso_svrl_for_xslt2.xsl -o:validation-xslt.xsl
(Note: You can get the iso_svrl_for_xslt2.xsl file from the official Schematron distribution package.)
Step 2: Validate XML with the Generated XSLT
Run the generated XSLT against your XML file to produce an SVRL report with validation results:
saxon -s:your-document.xml -xsl:validation-xslt.xsl -o:validation-results.xml
The validation-results.xml file will list all errors, warnings, and successful assertions from your Schematron rules.
3. Simplified Command-Line (Saxon PE/EE Only)
If you’re using Saxon’s commercial editions (Professional or Enterprise), you can skip the manual conversion step entirely. Saxon PE/EE supports direct Schematron validation via the -val flag:
saxon -s:your-document.xml -val:your-schema.sch -o:validation-results.xml
This command automatically handles the Schematron-to-XSLT conversion and runs the validation in one go.
4. Programmatic Approach (Java/.NET)
For Saxon PE/EE (Simpler)
You can load the Schematron directly as a schema using Saxon’s API:
import net.sf.saxon.s9api.*; import java.io.File; public class SchematronValidation { public static void main(String[] args) throws Exception { Processor processor = new Processor(false); SchemaManager schemaManager = processor.getSchemaManager(); // Load the Schematron file schemaManager.load(new StreamSource(new File("your-schema.sch"))); // Load the XML document to validate XdmNode xmlDoc = processor.newDocumentBuilder().build(new StreamSource(new File("your-document.xml"))); // Run validation SchemaValidator validator = schemaManager.newSchemaValidator(); try { validator.validate(xmlDoc); System.out.println("Validation passed!"); } catch (ValidationException e) { System.err.println("Validation failed: " + e.getMessage()); // Access detailed errors via e.getErrorList() } } }
For Saxon HE (Free Edition)
If you’re using Saxon HE, you’ll need to manually handle the Schematron-to-XSLT conversion first (using the same XSLT script as the command-line step), then execute the generated validation XSLT against your XML via the API.
Bonus: Make Results Human-Readable
The SVRL report is machine-friendly but can be transformed into a more readable format (like HTML) using another Schematron utility script, iso_svrl_report.xsl:
saxon -s:validation-results.xml -xsl:iso_svrl_report.xsl -o:validation-report.html
内容的提问来源于stack exchange,提问作者majid ismail

