c# - 是否可以将 schematron 架构编译成 Biztalk 程序集

标签 c# .net xsd biztalk schematron

是否可以创建 schematron 程序集,就像我们可以将 .xsd 模式编译成程序集并部署到 Biztalk 或其他应用程序(使用 BTSCompile 构建操作)一样?

例如,我们有一个从 HL7v3 模式构建的常规程序集,我有一个应用程序可以从程序集中加载模式作为 XmlSchema,并使用它来验证 XML。在这种情况下它工作正常。

这是我所说的基本概念:

    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;
    }

但是,如果我尝试对 Schematron 执行相同的操作,我将无法使用 BTSCompile Build Action 对其进行编译,我认为这是能够“查看”程序集中的模式所必需的。

目前我使用的 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 root element of a W3C XML Schema should be <schema> and its namespace should be 'http://www.w3.org/2001/XMLSchema'.

那么,当我按照它当然说的去做时:

The 'title' element is not supported in this context

因为它们不是有效的 xml 架构元素。所以现在我的问题是:有没有办法做我想在这里做的事情?我不是很精通 XML 模式,所以我可能忽略了一些简单的事情。

最佳答案

您可以使用 xs:annotation 元素将 schematron 规则嵌入到 XML 模式中(就像 Microsoft 对 BizTalk flat file schemas 所做的那样)。这将允许您将 schematron 规则编译到 BizTalk 程序集中。可以在 this older MSDN article 中找到示例模式.

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

您可以编写自定义管道组件来执行 schematron 验证,也许依赖于 the Schematron.net library .或者您可以使用开源管道组件,例如 the Schematron XmlValidator Pipeline Component for BizTalk (我自己没有用过)。如果您想编写一个验证整个 xml 文档的管道组件(而不是像默认的 XML 验证组件那样只在第一次错误时失败),请查看 Saravana Kumar 的 blog post on the matter .

关于c# - 是否可以将 schematron 架构编译成 Biztalk 程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13406412/

相关文章:

c# - 获取 DataTable 列数据类型

XML 简单类型、简单内容、复杂类型、复杂内容

xml - MarkLogic:XQuery 从 XML 文档中获取值?

xml - 从 xml 文档中读取命名空间

.net - 反别名模式差异?

c# - 是否可以从组件继承 Razor 标记?

c# - 从字符串中删除嵌入式 ASCII 控制字符

c# - MSIL代码调查

.net - 在云环境中记录 .NET 异常和消息有哪些选项?

c# - 如何编写自己的 AuthorizeTag?