package saxon.test;

import static org.junit.Assert.*;

import java.io.InputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.junit.Test;

import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;
import net.sf.saxon.s9api.XsltTransformer;

public class xslt {

	@Test
	public void test() throws TransformerException, SaxonApiException {
		//方式1：略复杂，但可配置能力强
		Processor proc = new Processor(false);
        XsltCompiler comp = proc.newXsltCompiler();
        XsltExecutable exp = comp.compile(new StreamSource(Class.class.getResourceAsStream("/styles/books.xsl")));
        XdmNode source = proc.newDocumentBuilder().build(new StreamSource(Class.class.getResourceAsStream("/data/books.xml")));
        
        Serializer out = proc.newSerializer(System.out);
        //Serializer out = proc.newSerializer(new File("books.html"));
        out.setOutputProperty(Serializer.Property.METHOD, "html");
        out.setOutputProperty(Serializer.Property.INDENT, "yes");
        XsltTransformer trans = exp.load();
        trans.setInitialContextNode(source);
        trans.setDestination(out);
        trans.transform();

		
		//方式2：最简单
		//创建一个转换工厂  
		//TransformerFactory tFactory = TransformerFactory.newInstance();  
		//用指定的XSLT样式单文件创建一个转换器  
		//Transformer transformer = tFactory.newTransformer(new StreamSource(Class.class.getResourceAsStream("/styles/books.xsl")));  
		//执行转换，并将转换后的目标文档作为响应输出  
		//transformer.transform(new StreamSource(Class.class.getResourceAsStream("/data/books.xml")), new StreamResult(System.out));  
	}

}

@@ -723,16 +723,17 @@
 oid test
+1
 () throw
@@ -776,16 +776,109 @@
 ption %7B%0A
+%09%09System.out.println(%22%5Cn%E3%80%8A--------------------%E4%BB%A5%E4%B8%8B%E4%B8%BA%E5%A2%9E%E5%BC%BA%E6%96%B9%E5%BC%8F---------------------------------%E3%80%8B%22);%0A%09%09%0A
 %09%09//%E6%96%B9%E5%BC%8F1%EF%BC%9A
@@ -1647,16 +1647,173 @@
 orm();%0A%0A
+%09%09%0A %0A%09%7D%0A%09%0A%09@Test%0A%09public void test2() throws TransformerException%7B%0A%09%09System.out.println(%22%5Cn%E3%80%8A--------------------%E4%BB%A5%E4%B8%8B%E4%B8%BA%E6%A0%87%E5%87%86%E6%96%B9%E5%BC%8F---------------------------------%E3%80%8B%22);%0A
 %09%09%0A%09%09//%E6%96%B9
@@ -1832,26 +1832,24 @@
 %E5%BB%BA%E4%B8%80%E4%B8%AA%E8%BD%AC%E6%8D%A2%E5%B7%A5%E5%8E%82  %0A%09%09
-//
 TransformerF
@@ -1931,18 +1931,16 @@
 %E8%BD%AC%E6%8D%A2%E5%99%A8  %0A%09%09
-//
 Transfor
@@ -2086,18 +2086,16 @@
 %E5%BA%94%E8%BE%93%E5%87%BA  %0A%09%09
-//
 transfor
@@ -2209,16 +2209,17 @@
 .out)); 
- 
+%09%09
 %0A%09%7D%0A%0A%7D%0A

seperate to 2 test