Bensonius
Bensonius

Reputation: 1541

Is it possible to compile a schematron schema into a Biztalk assembly

Is it possible to create schematron assemblies, the same way we can compile .xsd schemas into assemblies and deploy to a Biztalk, or other application (using the BTSCompile build action)?

For example we have a regular assembly that was built from the HL7v3 schemas and I have an app that loads the Schema as an XmlSchema from the assembly and uses it to validate XML against. It works fine in this case.

Here's a the basic idea of what I'm talking about:

    public static XmlSchema LoadSchema(System.Type schemaType)
    {
        if (schemaType == null)
        {
            throw new NullReferenceException("schemaType cannot be null. Pass a valid object type.");
        }

        XmlSchema schema = new XmlSchema();

        try
        {
            // Grabbing an Assembly that is loaded for the type we're after.
            Assembly schemaAssembly = Assembly.GetAssembly(schemaType);
            foreach (Type type in schemaAssembly.GetTypes())
            {
                if (typeof(SchemaBase).IsAssignableFrom(type) && !type.IsNested && type.Name == schemaType.Name)
                {
                    schema = (Activator.CreateInstance(type) as SchemaBase).Schema;
                    break;
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Could not Load Schema assembly.", ex);
        }

        return schema;
    }

However, if I try to do the same for a Schematron I cannot get it to compile using the BTSCompile Build Action which is what I'm assuming is required to be able to "see" the schemas within the assembly.

The Schematron file I'm using is basically this for now:

  <?xml version="1.0" encoding="utf-8"?>
<schema xmlns="http://www.ascc.net/xml/schematron" xmlns:sch="http://www.ascc.net/xml/schematron" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.ascc.net/xml/schematron http://www.ascc.net/xml/schematron/schematron1-5.xsd" xmlns:hl7="urn:hl7-org:v3">

<title>Schematron Rule Definitions</title>
<ns uri="urn:hl7-org:v3" prefix="hl7"/>
<ns uri="http://www.w3.org/2001/XMLSchema-instance" prefix="xsi"/>
<!-- Rules that pertain to multiple sections of the CDA -->
<pattern name="Header - Test">
    <rule context="/">
        <assert test="hl7:ClinicalDocument">
            ClinicalDocument must be the root node with the namespace urn:hl7-org:v3.
        </assert>
    </rule>
    </pattern>
</schema>

The error I'm receiving when trying to compile is:

The root element of a W3C XML Schema should be <schema> and its namespace should be 'http://www.w3.org/2001/XMLSchema'.

So, then when I do what it of course says:

The 'title' element is not supported in this context

because they aren't valid xml schema elements. So now my question is this: is there a way to do what I'm trying to do here? I'm not very proficient with XML Schemas, so it may be something simple I'm overlooking.

Upvotes: 2

Views: 801

Answers (1)

schellack
schellack

Reputation: 10274

You can embed schematron rules inside of an XML schema by making use of the xs:annotation element (like Microsoft does for BizTalk flat file schemas). That will allow you to compile the schematron rules into a BizTalk assembly. An example schema can be found in this older MSDN article.

BizTalk will ignore the annotations, however. If you want to make use of those rules, you will need to tell BizTalk how to do that.

You can write a custom pipeline component to do the schematron validation, perhaps relying on the Schematron.net library. Or you can use an open source pipeline component, like the Schematron XmlValidator Pipeline Component for BizTalk (I have not used it myself). If you want to write a pipeline component that validates the whole entire xml document (instead of just failing on the first error, as does the default XML Validation component), take a look at Saravana Kumar's blog post on the matter.

Upvotes: 1

Related Questions