2012-11-15 54 views
2

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

例如,我们有一个从HL7v3模式构建的常规程序集,我有一个应用程序从程序集中加载Schema作为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生成操作而这正是编译我假设需要能够“查看”程序集中的模式。

我使用的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 Schema不太熟练,所以它可能是我忽略的一些简单的东西。

+0

我重新启动了XRouter SchemaTron并添加了XPath2/XSLT2支持。查看https://github.com/gap777/SchemaTron – gap

回答

1

您可以通过使用xs:annotation元素(如Microsoft为BizTalk flat file schemas)将schematron规则嵌入到XML模式中。这将允许您将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

+1

我最终做到了嵌入式资源路由 - 因为Biztalk本身并不需要引用,而是一个辅助类。在我发现了XRouter schematron库[link](https://www.assembla.com/spaces/xrouter/wiki)之后,它比Schematron.net库(在这一点上变得相当老旧) )。 Xrouter库使用嵌入式资源(schematron文件),这比我以前的方式更适合我们的实现。 – Bensonius

相关问题