2014-10-02 61 views
0

我正在尝试使用saxon进行schematron验证。首先,我想将.sch文件编译为.xsl文件。后来,我想用首先生成的.xsl文件来验证.xml文件。在java代码中使用saxon困难.sch转换为.xsl

我发现像下面这样的撒克逊命令行用法。我成功地使用了它们。 但我需要使用java代码进行这些操作。 我尝试了下面的一些代码,但我没有猜到如何将sch扩展文件作为参数(edefter_yevmiye.sch)和iso_svrl_for_xslt2.xsl放入代码中。 我搜索了互联网,但我没有找到足够的信息。 是否有将.sch转换为.xsl的示例java代码,或者可以引导我吗?

我的Java代码

**Compiling .sch to .xsl** 

      net.sf.saxon.s9api.Processor processor1 = new net.sf.saxon.s9api.Processor(false); 
      net.sf.saxon.s9api.XsltCompiler xsltCompiler1 = processor1.newXsltCompiler(); 

      xsltCompiler1.setXsltLanguageVersion("2.0"); 
      xsltCompiler1.setSchemaAware(true); 

      net.sf.saxon.s9api.XsltExecutable xsltExecutable1 = xsltCompiler1.compile(new StreamSource(new FileInputStream(new File("File1.xsl")))); 

      net.sf.saxon.s9api.XsltTransformer xsltTransformer1 = xsltExecutable1.load(); 
      xsltTransformer1.setSource(new StreamSource(new FileInputStream(new 
         File("File2.sch")))); 


    **Validation** 

      net.sf.saxon.s9api.Processor processor2 = new net.sf.saxon.s9api.Processor(false); 
      net.sf.saxon.s9api.XsltCompiler xsltCompiler2 = processor2.newXsltCompiler(); 

      xsltCompiler2.setXsltLanguageVersion("2.0"); 
      xsltCompiler2.setSchemaAware(true); 

      net.sf.saxon.s9api.XsltExecutable xsltExecutable2 = xsltCompiler2.compile(new StreamSource(new 
         FileInputStream(new File(“File1.xslt")))); 

      net.sf.saxon.s9api.XsltTransformer xsltTransformer2 = xsltExecutable2.load(); 
      xsltTransformer2.setSource(new StreamSource(new FileInputStream(new 
         File("src.xml")))); 

      net.sf.saxon.s9api.Destination dest2 = new Serializer(System.out); 
      xsltTransformer2.setDestination(dest2); 


      xsltTransformer1.setDestination(xsltTransformer2); 
      xsltTransformer1.transform(); 

命令行模式

编译:

java -jar saxon9he.jar -o:output.xsl -s:some.sch iso_svrl_for_xslt2.xsl 

验证:

java -jar saxon9he.jar -o:warnings.xml -s:some.xml output.xsl 
+0

在萨克森的“resources”文件中有很多使用s9api接口的例子,称为samples/S9APIExamples.java,您可以从SourceForge或Saxonica站点下载该文件。你基本上已经明白了,如果失败并且您不了解失败,请告诉我们失败的原因。 – 2014-10-03 14:33:25

回答

0

您目前使用的第二次转型的目标为先,但是这意味着第一个转换器的输出被用作第二个源文件,而要我认为,将它用作第二次转换的样式表。

最简单的方法是为第一次转换设置一个XdmDestination,然后用这个目标对象执行destination.getXdmNode()。asSource()以获得输入给compile()方法第二次转型。