2014-02-27 22 views
3

我運行這段代碼:使用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。如果不是,你能指導我做一個簡單的樣本,它有所有的文件和作品嗎?

+0

嗯,首先,所有的反斜線看起來很可疑。 URI始終包含正斜槓,從不反斜槓。 –

+0

嘗試調用MemoryStream上的Close()。也許設置Position = 0會刪除現有的內容。 (我在這裏沒有專家,MemoryStream類一直困惑着我。) –

+0

我開始認爲我的xsl和/或sch和/或xml文件在某些​​方面有缺陷。我發現了「iso-schematron-xslt2」,它使用[link]中提到的「http://purl.oclc.org/dsdl/schematron」命名空間http://stackoverflow.com/questions/16942939/saxon-produces-空-XML的從-的Schematron?RQ = 1。但是,我使用這些文件獲得相同的行爲。不幸的是,iso-schematron-xslt2文件集不包含sch和xml的已知好樣本來測試。任何人都可以指給我一個這樣的例子嗎? –

回答

2

分辨率:

serializer.SetOutputStream(st2); 

應該

serializer2.SetOutputStream(st2); 
+1

我添加了一個完整的演示解決方案:https://onedrive.live.com/redir?resid=DC00B7D9CE3B0F3E!19754&authkey=!AA4lQ76Eem8ZBIU&ithint=file%2c.zip –

+0

+adamındibisin dibi。 –

5

我真正的根本問題是尋找簡單完整的示例代碼做的Schematron在.NET。所以對於下一個人來說,這是我正在尋找的樣本。我試圖儘可能地做到這一點。如果我錯過了什麼,請發表評論。

  1. 創建單元測試項目
  2. 運行的NuGet命令
  3. 下載文件Schematron的
  4. 使用附帶類和SCH,XML文件。
  5. 運行測試程序

的NuGet撒克遜命令行:

Install-Package Saxon-HE 

下載到目前爲止的Schematron文件 http://www.schematron.com/tmp/iso-schematron-xslt2.zip

單元測試

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

     } 
    } 
} 

Transform類:

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; 


     } 

    } 
} 

的Schematron文件

<?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文件

<?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> 

結果:

<?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>