Csharprules

Validate a LIXI2 Package Using Schematron Rules (for C# and .NET)

Validate a LIXI2 Package Using Schematron Rules (for C# and .NET)

Posted on: May 28, 2020


Schematron allows us to validate LIXI2 messages against business rules. While the LIXI2 schemas are excellent tools for validating message structure and providing a framework for standardised messaging between systems. Schematron gives us a way to define more complex business rules in a format that is distributable. Rather than providing a .pdf document to developers explaining the payload that is required by your system, a Schematron rule sheet can provide the same functionality in a machine readable format that can more easily be consumed by an automated system.

The framework we will be using to execute the Schematron validation uses The Schematron Skeleton Implementation. This framework does not require a Schematron validation engine, it simply uses a generic XSLT engine to execute an XSL transformation. The process to validate a LIXI2 message with Schematron is very straight forward and is explained in detail below.

what is schematron? A language for making assertions about the presence or absence of patterns in XML documents.
 

what is schematron used for? Business rules validation, data reporting, general validation, quality control, quality assurance, firewalling, filtering, constraint checking, naming and design rules checking, statistical consistency, data exploration, transformation testing, feature extraction, house-style-rules checking
 

what makes schematron unique? Schematron is very simple (only five important elements), very powerful (it can express many kinds of constraints impossible in other schema languages), very diverse (it can be used for business rules, reports as well as the kinds of static constraints usually associated with schemas.)
 
- schematron.com
 

To validate a LIXI2 message with Schematron we will follow these three steps:

  1. Download a Schematron rule sheet from LIXILab
  2. Validate a LIXI2 message against the Schematron Schema
  3. Examine the Schematron Validation Report

Download a LIXI2 Schematron Schema from LIXILab

Sample LIXI2 Schematron schemas can be downloaded from our LIXILab Schematron Demo project (available to logged in members). Alternatively, you can write your own Schematron schema rules to meet your particular requirements. The Schematron website provides An Overview of Schematron with instructions on how to write your own Schematron rules.

Note: we will be using the XSLT version of our Schematron rule sheet (with the ".xsl" file extension) not the Schematron schema files (with the ".sch" file extension)

Here is a direct link to the MASTER Schematron Rule.xsl file. This rule sheet contains the complete set of LIXI2 Schematron rules.

Validate a LIXI2 message against the Schematron Schema (using C# and .NET)

Here is a C# .NET method to execute a transformation using XSLT. This method will be used to validate our LIXI2 message against the Schematron XSLT document. A Schematron validation report will be written to a file with the specified filename.

static void Transform (String document_filename,
                      String xslt_filename,
                      String result_filename)
{
    XPathDocument myXPathDoc = new XPathDocument(document_filename);
    XslCompiledTransform myXslTrans = new XslCompiledTransform();
    myXslTrans.Load(xslt_filename);
    XmlTextWriter myWriter = new XmlTextWriter(result_filename, null);
    myXslTrans.Transform(myXPathDoc, null, myWriter);
}

This is how we will call our transformation function:

Transform("joint-application.xml","MASTER Schematron Rule.xsl","validation_report.svrl");

Examine the Schematron Validation Report

The output of this function, a Schematron validation report, will be written to a file with the specified filename. The report will include the details of any failed rules including:

  • the location of the invalid data within the LIXI2 message, expressed as an XPath.
  • the technical details of the test that has failed
  • the diagnostic message that should be displayed to the end user

This excerpt from a validation report shows that the second applicant in our credit application package has not satisfied the requirement that they must provide at least two different types of proof of identity document.

<svrl:failed-assert location="/Package/Content/Application/PersonApplicant[2]">
    <svrl:text>A person applicant must provide at least two different types of 
proof of identity document.</svrl:text> </svrl:failed-assert>

Complete C# Code to Execute a Schematron Validation

Here is the full code required to execute the Validation:

using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace SchematronDemo
{
    class Program
    {

        static void Main(string[] args)
        {
            Transform("joint-application.xml", 
                      "MASTER Schematron Rule.xsl", 
                      "validation_report.svrl");
        }

        static void Transform(String document_filename,
                             String xslt_filename,
                             String result_filename)
        {
            XPathDocument myXPathDoc = new XPathDocument(document_filename);
            XslCompiledTransform myXslTrans = new XslCompiledTransform();
            myXslTrans.Load(xslt_filename);
            XmlTextWriter myWriter = new XmlTextWriter(result_filename, null);
            myXslTrans.Transform(myXPathDoc, null, myWriter);
        }
    }
}

Related Posts

For an overview of the structure of a LIXI2 Credit Application read our LIXI2 Credit Application Tutorials.

If you need to validate LIXI2 messages against Schematron rules using Python check out this blog post: Validate a LIXI2 Message against Business Rules using Schematron (for Python).

If you need to validate LIXI2 messages against Schematron rules using Java check out this blog post: Validate a LIXI2 Package Using Schematron Rules (for Java and Eclipse)

Feel free to contact any member of the LIXI team (or use our contact form) and we will be happy to help with more information.


Written by:
John Matthews, LIXI Technical Lead
First Published: May 28, 2020