2

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.

4

1 回答 1

1

您可以通过使用 xs:annotation 元素将 schematron 规则嵌入 XML 架构中(就像 Microsoft 为BizTalk 平面文件架构所做的那样)。这将允许您将 schematron 规则编译到 BizTalk 程序集中。可以在这篇较早的 MSDN 文章中找到示例架构。

但是,BizTalk 将忽略注释。如果您想使用这些规则,您需要告诉 BizTalk 如何做到这一点。

您可以编写一个自定义管道组件来进行 schematron 验证,可能依赖于Schematron.net 库。或者您可以使用开源管道组件,例如BizTalk 的 Schematron XmlValidator 管道组件(我自己没有使用过)。如果您想编写一个验证整个 xml 文档的管道组件(而不是像默认的 XML 验证组件那样仅在第一个错误上失败),请查看 Saravana Kumar 的博客文章

于 2012-11-28T20:23:53.607 回答