简体   繁体   English

是否可以将schematron模式编译为Biztalk程序集

[英]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)? 是否可以创建schematron程序集,就像我们将.xsd模式编译为程序集并部署到Biztalk或其他应用程序一样(使用BTSCompile构建操作)?

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. 例如,我们有一个基于HL7v3模式构建的常规程序集,而我有一个应用程序,可以从该程序集中将该模式作为XmlSchema加载并使用它来验证XML。 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. 但是,如果我尝试对Schematron执行相同的操作,则无法使用BTSCompile Build Action对其进行编译,这是我假设必须能够“查看”程序集中的模式。

The Schematron file I'm using is basically this for now: 我现在使用的Schematron文件基本上是这样的:

  <?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. 因为它们不是有效的xml架构元素。 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. 我对XML模式不是很熟练,所以我可能忽略了一些简单的事情。

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 ). 您可以通过使用xs:annotation元素将schematron规则嵌入XML模式中(就像Microsoft的BizTalk平面文件模式一样 )。 That will allow you to compile the schematron rules into a BizTalk assembly. 这将使您可以将schematron规则编译为BizTalk程序集。 An example schema can be found in this older MSDN article . 此较早的MSDN文章中可以找到示例架构。

BizTalk will ignore the annotations, however. 但是,BizTalk将忽略注释。 If you want to make use of those rules, you will need to tell BizTalk how to do that. 如果要利用这些规则,则需要告诉BizTalk如何做到这一点。

You can write a custom pipeline component to do the schematron validation, perhaps relying on the Schematron.net library . 您可以编写一个自定义管道组件来执行schematron验证,也许依赖于Schematron.net库 Or you can use an open source pipeline component, like the Schematron XmlValidator Pipeline Component for BizTalk (I have not used it myself). 或者,您可以使用开源管道组件,例如BizTalk的Schematron XmlValidator管道组件 (我自己没有使用过)。 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 . 如果您想编写一个用于验证整个xml文档的管道组件(而不是像第一个错误一样失败,默认的XML Validation组件也不例外),请查看Saravana Kumar 关于此事博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM