将 SAXON 9.5 (nuget) 与 Schematron 结合使用

2024-01-30

我正在运行这段代码:

        string path = AppDomain.CurrentDomain.BaseDirectory;

        // Uri schemaUri = new Uri(@"file:\\" + path + @"\sch\patient.sch");
        Uri totransformEE = new Uri(@"file:\\" + path + @"\po\po-schema.sch");
        Uri transformER = new Uri(@"file:\\" + path + @"\xsl\conformance1-5.xsl");

        ///////////////////////////////
        // Crate Schemtron xslt to be applied
        ///////////////////////////////
        // Create a Processor instance.
        Processor processor = new Processor();

        // Load the source document
        XdmNode input = processor.NewDocumentBuilder().Build(totransformEE);

        // Create a transformer for the stylesheet.
        XsltTransformer transformer = processor.NewXsltCompiler().Compile(transformER).Load();

        // Set the root node of the source document to be the initial context node
        transformer.InitialContextNode = input;

        // Create a serializer
        Serializer serializer = new Serializer();
        MemoryStream st = new MemoryStream();
        serializer.SetOutputStream(st);

        // Transform the source XML to System.out.
        transformer.Run(serializer);

        st.Position = 0;
        System.IO.StreamReader rd = new System.IO.StreamReader(st);
        string xsltSchematronStylesheet = rd.ReadToEnd();

        System.Diagnostics.Debug.WriteLine(xsltSchematronStylesheet);

        // Load the source document
        Uri transformEE2 = new Uri(@"file:\\" + path + @"\po\po-bad.xml");

        var documentbuilder2 = processor.NewDocumentBuilder();
        XdmNode input2 = documentbuilder2.Build(transformEE2);

        ////// Create a transformer for the stylesheet.
        StringReader sr2 = new StringReader(xsltSchematronStylesheet);
        XsltTransformer transformer2 = processor.NewXsltCompiler().Compile(sr2).Load();

        // Set the root node of the source document to be the initial context node
        transformer2.InitialContextNode = input2;

        // Create a serializer
        Serializer serializer2 = new Serializer();
        MemoryStream st2 = new MemoryStream();
        serializer.SetOutputStream(st2);

        transformer2.MessageListener = new MyMessageListener();
        // Transform the source XML to System.out.
        transformer2.Run(serializer2);

        st2.Position = 0;
        System.IO.StreamReader rd2 = new System.IO.StreamReader(st2);
        string xsltSchematronResult = rd2.ReadToEnd();
        System.Diagnostics.Debug.WriteLine(xsltSchematronResult);

在检查 xsltSchematronStylesheet 时,我得到了一个似乎是 XSLT 文件的文件。然而,st2 末尾的流的长度为 0。另外,MyMessageListener.Message 不接收任何调用(我使用了断点)。

我不确定我是否有错误的代码、错误的示例文件等。 我相信我的示例文件是正确的,但也许我有坏的或丢失了一些。

有谁知道为什么没有数据返回到流 st2.如果没有,您能否指导我一个包含所有文件且有效的简单示例?


我真正的根本问题是找到简单完整的示例代码来在.Net 中执行 Schematron。所以对于下一个人来说,这是我正在寻找的样本。我已尽力使其尽可能完整。如果我错过了什么,请发表评论。

  1. 创建单元测试项目
  2. 运行 Nuget 命令
  3. 下载 Schematron 文件
  4. 使用包含的类和 sch、xml 文件。
  5. 运行测试程序

Nuget 撒克逊命令行:

Install-Package Saxon-HE 

下载最新的 Schematron 文件 http://www.schematron.com/tmp/iso-schematron-xslt2.zip http://www.schematron.com/tmp/iso-schematron-xslt2.zip

UnitTest:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace SOAPonFHIR.Test
{
    [TestClass]
    public class Schematron
    {
        [TestMethod]
        public void XSLT_SAXON_Simple_Schematron2()
        {

            ///////////////////////////////
            // Transform original Schemtron  
            ///////////////////////////////
            string path = AppDomain.CurrentDomain.BaseDirectory;

            Uri schematron = new Uri(@"file:\\" + path + @"\simple\input.sch");
            Uri schematronxsl = new Uri(@"file:\\" + path + @"\xsl_2.0\iso_svrl_for_xslt2.xsl");

            Stream schematrontransform = new Test.XSLTransform().Transform(schematron, schematronxsl);

            ///////////////////////////////
            // Apply Schemtron xslt 
            ///////////////////////////////
            FileStream xmlstream = new FileStream(path + @"\simple\input.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
            Stream results = new Test.XSLTransform().Transform(xmlstream, schematrontransform);

            System.Diagnostics.Debug.WriteLine("RESULTS");
            results.Position = 0;
            System.IO.StreamReader rd2 = new System.IO.StreamReader(results);
            string xsltSchematronResult = rd2.ReadToEnd();
            System.Diagnostics.Debug.WriteLine(xsltSchematronResult);

        }
    }
}

变换类:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using Saxon.Api;
using System.IO;
using System.Xml.Schema;
using System.Collections.Generic;

namespace SOAPonFHIR.Test
{
    public class XSLTransform
    {
        public Stream Transform(Uri xmluri, Uri xsluri)
        {


            // Create a Processor instance.
            Processor processor = new Processor();

            // Load the source document
            XdmNode input = processor.NewDocumentBuilder().Build(xmluri);

            // Create a transformer for the stylesheet.
            var compiler = processor.NewXsltCompiler();
            compiler.ErrorList = new System.Collections.Generic.List<Exception>();

            XsltTransformer transformer = compiler.Compile(xsluri).Load();

            if (compiler.ErrorList.Count != 0)
                throw new Exception("Exception loading xsl!");

            // Set the root node of the source document to be the initial context node
            transformer.InitialContextNode = input;

            // Create a serializer
            Serializer serializer = new Serializer();
            MemoryStream results = new MemoryStream();
            serializer.SetOutputStream(results);

            // Transform the source XML to System.out.
            transformer.Run(serializer);

            //get the string
            results.Position = 0;
            return results;


        }

        public System.IO.Stream Transform(System.IO.Stream xmlstream, System.IO.Stream xslstream)
        {

            // Create a Processor instance.
            Processor processor = new Processor();

            // Load the source document
            var documentbuilder = processor.NewDocumentBuilder();
            documentbuilder.BaseUri = new Uri("file://c:/" );
            XdmNode input = documentbuilder.Build(xmlstream);

            // Create a transformer for the stylesheet.
            var compiler = processor.NewXsltCompiler();
            compiler.ErrorList = new System.Collections.Generic.List<Exception>();
            compiler.XmlResolver = new XmlUrlResolver();
            XsltTransformer transformer = compiler.Compile(xslstream).Load();

            if (compiler.ErrorList.Count != 0)
                throw new Exception("Exception loading xsl!");

            // Set the root node of the source document to be the initial context node
            transformer.InitialContextNode = input;

            // Create a serializer
            Serializer serializer = new Serializer();
            MemoryStream results = new MemoryStream();
            serializer.SetOutputStream(results);

            // Transform the source XML to System.out.
            transformer.Run(serializer);

            //get the string
            results.Position = 0;
            return results;


        }

    }
}

架构文件

<?xml version="1.0" encoding="utf-8"?>
<iso:schema
  xmlns="http://purl.oclc.org/dsdl/schematron" 
  xmlns:iso="http://purl.oclc.org/dsdl/schematron"
  xmlns:dp="http://www.dpawson.co.uk/ns#"
  queryBinding='xslt2'
  schemaVersion='ISO19757-3'>

  <iso:title>Test ISO schematron file. Introduction mode</iso:title>
  <iso:ns prefix='dp' uri='http://www.dpawson.co.uk/ns#'/> 

  <iso:pattern>
    <iso:rule context="chapter">

      <iso:assert
         test="title">A chapter should have a title</iso:assert>  
    </iso:rule>
  </iso:pattern>


</iso:schema>

XML File

<?xml version="1.0" encoding="utf-8" ?>
<doc>
  <chapter id="c1">
    <title>chapter title</title>  
    <para>Chapter content</para>
  </chapter>

  <chapter id="c2">
    <title>chapter 2 title</title>
    <para>Content</para>           
  </chapter>

  <chapter id="c3">
    <title>Title</title>
    <para>Chapter 3 content</para>
  </chapter>
</doc>

Results:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<svrl:schematron-output xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
                        xmlns:xs="http://www.w3.org/2001/XMLSchema"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                        xmlns:saxon="http://saxon.sf.net/"
                        xmlns:schold="http://www.ascc.net/xml/schematron"
                        xmlns:iso="http://purl.oclc.org/dsdl/schematron"
                        xmlns:xhtml="http://www.w3.org/1999/xhtml"
                        xmlns:dp="http://www.dpawson.co.uk/ns#"
                        title="Test ISO schematron file. Introduction mode"
                        schemaVersion="ISO19757-3"><!--   
           
           
         -->
   <svrl:ns-prefix-in-attribute-values uri="http://www.dpawson.co.uk/ns#" prefix="dp"/>
   <svrl:active-pattern document="file:///c:/"/>
   <svrl:fired-rule context="chapter"/>
   <svrl:fired-rule context="chapter"/>
   <svrl:fired-rule context="chapter"/>
</svrl:schematron-output>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 SAXON 9.5 (nuget) 与 Schematron 结合使用 的相关文章

  • 如何让 Swagger 插件在自托管服务堆栈中工作

    我已经用 github 上提供的示例重新提出了这个问题 并为任何想要自己运行代码的人提供了一个下拉框下载链接 Swagger 无法在自托管 ServiceStack 服务上工作 https stackoverflow com questio
  • 如何将非静态类成员“std::bind”绑定到 Win32 回调函数“WNDPROC”?

    我正在尝试将非静态类成员绑定到标准WNDPROC http msdn microsoft com en us library ms633573 aspx功能 我知道我可以通过将类成员设为静态来简单地做到这一点 但是 作为一名 C 11 ST
  • 提交后禁用按钮

    当用户提交付款表单并且发布表单的代码导致 Firefox 中出现重复发布时 我试图禁用按钮 去掉代码就不会出现这个问题 在firefox以外的任何浏览器中也不会出现这个问题 知道如何防止双重帖子吗 System Text StringBui
  • 在 DataView 的 RowFilter 中选择 DISTINCT

    我试图根据与另一个表的关系缩小 DataView 中的行范围 我使用的 RowFilter 如下 dv new DataView myDS myTable id IN SELECT DISTINCT parentID FROM myOthe
  • 复制 std::function 的成本有多高?

    While std function是可移动的 但在某些情况下不可能或不方便 复制它会受到重大处罚吗 它是否可能取决于捕获变量的大小 如果它是使用 lambda 表达式创建的 它依赖于实现吗 std function通常被实现为值语义 小缓
  • 在 C 中匹配二进制模式

    我目前正在开发一个 C 程序 需要解析一些定制的数据结构 幸运的是我知道它们是如何构造的 但是我不确定如何在 C 中实现我的解析器 每个结构的长度都是 32 位 并且每个结构都可以通过其二进制签名来识别 举个例子 有两个我感兴趣的特定结构
  • 复制目录内容

    我想将目录 tmp1 的内容复制到另一个目录 tmp2 tmp1 可能包含文件和其他目录 我想使用C C 复制tmp1的内容 包括模式 如果 tmp1 包含目录树 我想递归复制它们 最简单的解决方案是什么 我找到了一个解决方案来打开目录并读
  • 使用 Newtonsoft 和 C# 反序列化嵌套 JSON

    我正在尝试解析来自 Rest API 的 Json 响应 我可以获得很好的响应并创建了一些类模型 我正在使用 Newtonsoft 的 Json Net 我的响应中不断收到空值 并且不确定我的模型设置是否正确或缺少某些内容 例如 我想要获取
  • 为什么调用非 const 成员函数而不是 const 成员函数?

    为了我的目的 我尝试包装一些类似于 Qt 共享数据指针的东西 经过测试 我发现当应该调用 const 函数时 会选择它的非 const 版本 我正在使用 C 0x 选项进行编译 这是一个最小的代码 struct Data int x con
  • 是否有实用的理由使用“if (0 == p)”而不是“if (!p)”?

    我倾向于使用逻辑非运算符来编写 if 语句 if p some code 我周围的一些人倾向于使用显式比较 因此代码如下所示 if FOO p some code 其中 FOO 是其中之一false FALSE 0 0 0 NULL etc
  • 从 Linux 内核模块中调用用户空间函数

    我正在编写一个简单的 Linux 字符设备驱动程序 以通过 I O 端口将数据输出到硬件 我有一个执行浮点运算的函数来计算硬件的正确输出 不幸的是 这意味着我需要将此函数保留在用户空间中 因为 Linux 内核不能很好地处理浮点运算 这是设
  • C# HashSet 只读解决方法

    这是示例代码 static class Store private static List
  • C# 中的合并运算符?

    我想我记得看到过类似的东西 三元运算符 http msdn microsoft com en us library ty67wk28 28VS 80 29 aspx在 C 中 它只有两部分 如果变量值不为空 则返回变量值 如果为空 则返回默
  • CMake 无法确定目标的链接器语言

    首先 我查看了this https stackoverflow com questions 11801186 cmake unable to determine linker language with c发帖并找不到解决我的问题的方法 我
  • 使用管道时,如果子进程数量大于处理器数量,进程是否会被阻塞?

    当子进程数量很大时 我的程序停止运行 我不知道问题是什么 但我猜子进程在运行时以某种方式被阻止 下面是该程序的主要工作流程 void function int process num int i initial variables for
  • 使用 %d 打印 unsigned long long

    为什么我打印以下内容时得到 1 unsigned long long int largestIntegerInC 18446744073709551615LL printf largestIntegerInC d n largestInte
  • 调用堆栈中的“外部代码”是什么意思?

    我在 Visual Studio 中调用一个方法 并尝试通过检查调用堆栈来调试它 其中一些行标记为 外部代码 这到底是什么意思 方法来自 dll已被处决 外部代码 意味着该dll没有可用的调试信息 你能做的就是在Call Stack窗口中单
  • 方法优化 - C#

    我开发了一种方法 允许我通过参数传入表 字符串 列数组 字符串 和值数组 对象 然后使用这些参数创建参数化查询 虽然它工作得很好 但代码的长度以及多个 for 循环散发出一种代码味道 特别是我觉得我用来在列和值之间插入逗号的方法可以用不同的
  • 当从finally中抛出异常时,Catch块不会被评估

    出现这个问题的原因是之前在 NET 4 0 中运行的代码在 NET 4 5 中因未处理的异常而失败 部分原因是 try finallys 如果您想了解详细信息 请阅读更多内容微软连接 https connect microsoft com
  • 如何将 PostgreSql 与 EntityFramework 6.0.2 集成? [复制]

    这个问题在这里已经有答案了 我收到以下错误 实体框架提供程序类型的 实例 成员 Npgsql NpgsqlServices Npgsql 版本 2 0 14 2 文化 中性 PublicKeyToken 5d8b90d52f46fda7 没

随机推荐