将SAXON 9.5(nuget)与Schematron一起使用

我正在运行此代码:

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. 创建一个unit testing项目
  2. 运行Nuget命令
  3. 下载Schematron文件
  4. 使用包含的类和sch,xml文件。
  5. 运行测试程序

Nuget Saxon Commandline:

 Install-Package Saxon-HE 

下载最新的Schematron文件 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(); 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(); 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; } } } 

Schematron文件

   Test ISO schematron file. Introduction mode    A chapter should have a title    

XML文件

    chapter title Chapter content   chapter 2 title Content   Title Chapter 3 content   

结果:

         

解析度:

 serializer.SetOutputStream(st2); 

应该

 serializer2.SetOutputStream(st2);