FreeXmlToolkit

Schematron Support

FreeXmlToolkit provides comprehensive Schematron support for advanced XML validation beyond basic XSD schema validation. Schematron allows you to define business rules and custom validation constraints using XPath expressions.

What is Schematron?

Schematron is a rule-based validation language that expresses constraints and validation rules for XML documents using XPath expressions. Unlike XSD, which focuses on structure and data types, Schematron excels at expressing business logic and complex validation scenarios.

Key Features

1. Visual Schematron Rule Builder

2. Schematron Code Editor

3. Integration with XML Editor

Schematron Components

Rules and Patterns

<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
    <sch:pattern id="business-rules">
        <sch:title>Business Validation Rules</sch:title>
        
        <sch:rule context="invoice">
            <sch:assert test="@date">
                Invoice must have a date attribute
            </sch:assert>
            
            <sch:assert test="sum(item/price) = total">
                Total must equal sum of item prices
            </sch:assert>
        </sch:rule>
        
        <sch:rule context="item">
            <sch:assert test="price > 0">
                Item price must be greater than zero
            </sch:assert>
        </sch:rule>
    </sch:pattern>
</sch:schema>

Validation Types

Visual Rule Builder

Rule Creation Workflow

  1. Define Context: Select XML elements to validate
  2. Create Conditions: Build XPath expressions for validation logic
  3. Set Messages: Define error and warning messages
  4. Test Rules: Validate against sample XML documents
  5. Export Schema: Generate complete Schematron schema

Template Library

The visual builder includes templates for common scenarios:

Advanced Features

XPath Expression Builder

Testing and Debugging

Documentation Generation

Generate comprehensive HTML documentation from Schematron schemas:

<sch:rule context="invoice">
    <sch:title>Invoice Validation</sch:title>
    <sch:p>This rule validates invoice business logic including dates, amounts, and required fields.</sch:p>
    
    <sch:assert test="@date">
        <sch:title>Date Required</sch:title>
        All invoices must have a date attribute in YYYY-MM-DD format.
    </sch:assert>
</sch:rule>

Integration Examples

XML Editor Integration

// Schematron validation in XML Editor
public class SchematronXmlIntegrationService {
    public ValidationResult validateXmlWithSchematron(
            Document xmlDoc, 
            File schematronFile) {
        // Load and compile Schematron schema
        // Validate XML document
        // Return detailed validation results
    }
}

XSD + Schematron Validation

Combine both validation approaches for comprehensive XML validation:

  1. Structure Validation: XSD validates XML structure and data types
  2. Business Rules: Schematron validates business logic and constraints
  3. Combined Results: Unified error reporting from both validation types

Schematron File Management

File Operations

Schema Organization

Performance Optimization

Efficient Rule Design

Caching and Processing

Common Use Cases

Financial Documents

<sch:pattern id="financial-validation">
    <sch:rule context="transaction">
        <sch:assert test="amount > 0">
            Transaction amount must be positive
        </sch:assert>
        
        <sch:assert test="currency = /document/baseCurrency">
            All transactions must use base currency
        </sch:assert>
    </sch:rule>
</sch:pattern>

Data Consistency

<sch:pattern id="data-consistency">
    <sch:rule context="person">
        <sch:assert test="age >= 18 or guardian">
            Persons under 18 must have a guardian
        </sch:assert>
        
        <sch:assert test="count(//person[@id=current()/@id]) = 1">
            Person ID must be unique
        </sch:assert>
    </sch:rule>
</sch:pattern>

Best Practices

Rule Design

  1. Clear Messages: Write descriptive error messages
  2. Specific Context: Use precise context selection
  3. Performance Aware: Design rules for efficiency
  4. Maintainable: Keep rules simple and well-documented

Schema Organization

  1. Modular Design: Group related rules in patterns
  2. Reusable Components: Create reusable rule libraries
  3. Version Control: Track schema changes over time
  4. Documentation: Document rule purpose and usage

Troubleshooting

Common Issues

Debugging Tools


Previous: Context-Sensitive IntelliSense Home Next: Schema Support