将 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 结合使用 的相关文章

  • 实体框架中的重复键异常?

    我试图捕获当我将具有给定用户名的现有用户插入数据库时 引发的异常 正如标题所说 我正在使用 EF 当我尝试将用户插入数据库时 引发的唯一异常是 UpdateException 如何提取此异常以识别其是否是重复异常或其他异常 catch Up
  • ProtoBuf-net AsReference 需要 Activator.CreateInstance 中的公共构造函数吗?

    在我的两门课程中 看起来像这样 最少 using System using System Collections Generic using System Collections using System ComponentModel us
  • C++ - 模板专业化和部分专业化

    我一直在互联网和 stackoverflow 上寻找具体的答案 但我似乎找不到 我必须创建一个通用类 然后实现特定的功能 我的具体说明是 您需要使用模板表达式参数以及模板类专业化和部分专业化 我有一个模板类 template
  • WPF - 按多列排序时使用自定义比较器

    我有一个 ListView GridView 我想按 2 列排序 因此如果第 1 列中有 2 个以上的项目具有相同的值 它将按第 2 列排序 非常简单 但是在对 A Z 进行排序时 空字符串会出现在顶部 我想把它们移到底部 我制作了一个比较
  • 浏览器收集哪些值作为回发数据?

    当页面被发送回服务器时 浏览器收集每个控件的当前值并将其粘贴到一个字符串中 然后 该回发数据通过 HTTP POST 发送回服务器 Q1 除了控件的 Text 属性和 SelectedIndexchanged 因此除了用户输入数据 之外 控
  • C# ConfigurationManager 从 app.config 检索错误的连接字符串

    我有一个简单的 WinForms 应用程序 它最终将成为一个游戏 现在 我正在研究它的数据访问层 但遇到了障碍 我创建了一个单独的项目 名为DataAccess在其中 我创建了一个本地 mdfSQL Server 数据库文件 我还创建了一个
  • 未定义异常变量时通过引用捕获

    捕获异常时 标准指导是按值抛出 按引用捕获 据我了解 这有两个原因 如果由于内存不足异常而引发异常 我们将不会调用可能终止程序的复制构造函数 如果异常是继承层次结构的一部分 我们可能会对异常进行对象切片 如果我们有一个场景 我们没有在 ca
  • 我们应该使用 Eval 还是 Databind 事件?

    当使用 Asp Net 并使用 ListView 等控件创建网站时 使用 Eval 命令是一个好习惯吗 还是应该在 databind 事件中填充文字和数据 取决于您是否想在更新事件上写回数据 在这种情况下数据绑定 如果您只想读取该数据 可以
  • C++ 析构函数:何时释放内存?

    如果我删除一个导致其析构函数被调用的对象 那么内存是在析构函数完成函数中的任何操作之前还是之后被释放 仅当最小派生类子对象被销毁后才会释放内存 所以如果你有 class Base class Derived public Base publ
  • IClaimsTransformation 未触发

    我尝试过实施一个IClaimsTransformation我在 ASP NET CORE 3 1 Web 应用程序中找到的类 public class ClaimsTransformer IClaimsTransformation publ
  • ASP MVC 5 - 403 customError 不起作用

    我正在尝试为我的应用程序创建自定义错误页面 它在大部分情况下都有效 但不适用于403 errors 我的网络配置
  • 从 ef core 的子集合中删除一些项目

    我有一个父表和子表 其中父表与子表具有一对多关系 我想删除一些子项 并且希望父项的子集合反映该更改 如果我使用删除选定的子项RemoveRange 那么子集合不会更新 如果我使用Remove从子集合中删除子集合然后 显然 它不如使用效率高R
  • IBM Watson 对话服务错误:无法从“方法组”转换为“conversation.onMessage”

    我正在尝试运行 IBM Watson会话服务团结和下面是代码片段 https github com watson developer cloud unity sdk conversation private Conversation m C
  • ASP.NET Web API Swagger(Swashbuckle)重复OperationId

    I have a web api controller like below In swagger output I am having the below image And when I want to consume it in my
  • 打破条件变量死锁

    我遇到这样的情况 线程 1 正在等待条件变量 A 该变量应该由线程 2 唤醒 现在线程 2 正在等待条件变量 B 该变量应该由线程 1 唤醒 在我使用的场景中条件变量 我无法避免这样的死锁情况 我检测到循环 死锁 并终止死锁参与者的线程之一
  • 为什么 GCC 6.3 在没有显式 C++11 支持的情况下编译此 Braced-Init-List 代码?

    我有一个问题大括号括起来的列表的不同含义 https stackoverflow com q 37682392 2642059 我知道C 03不支持C 11initializer list 然而 即使没有 std c 11编译器标志 gcc
  • .NET 的 HttpWebResponse 是否会自动解压缩 GZiped 和 Deflated 响应?

    我正在尝试执行一个接受压缩响应的请求 var request HttpWebRequest HttpWebRequest Create requestUri request Headers Add HttpRequestHeader Acc
  • 向每个收件人发送一封包含不同内容的电子邮件(使用抄送字段)

    在你因为这个问题 毫无意义 和 不可能 而驳回之前 请听我说完 问题 我们在使用我们的系统发送的每封电子邮件中实施跟踪像素 即具有唯一 URL 的可下载 GIF 文件 这有助于我们跟踪电子邮件的打开情况 问题是 当我们抄送一些收件人时 跟踪
  • Visual Studio 2015默认附加库

    当我在 VS 2015 中创建一个空项目时 它会自动将这些库放入 附加依赖项 中 kernel32 lib user32 lib gdi32 lib winspool lib comdlg32 lib advapi32 lib shell3
  • 编译器什么时候内联函数?

    在 C 中 函数仅在显式声明时才内联inline 或在头文件中定义 或者编译器是否允许内联函数 因为他们认为合适 The inline关键字实际上只是告诉链接器 或告诉编译器告诉链接器 同一函数的多个相同定义不是错误 如果您想在标头中定义函

随机推荐