1 7 8 package org.dom4j; 9 10 import junit.textui.TestRunner; 11 12 import java.util.List ; 13 14 import javax.xml.transform.Source ; 15 import javax.xml.transform.Transformer ; 16 import javax.xml.transform.TransformerFactory ; 17 import javax.xml.transform.stream.StreamSource ; 18 19 import org.dom4j.io.DocumentResult; 20 import org.dom4j.io.DocumentSource; 21 22 28 public class XSLTTest extends AbstractTestCase { 29 public static void main(String [] args) { 30 TestRunner.run(XSLTTest.class); 31 } 32 33 public void testTransform() throws Exception { 36 Document transformedDoc = transform("/xml/nitf/ashtml.xsl"); 37 38 assertTrue("Transformed Document is not null", transformedDoc != null); 40 41 List h1List = transformedDoc.selectNodes("/html//h1"); 42 43 assertTrue("At least one <h1>", h1List.size() > 0); 44 45 List pList = transformedDoc.selectNodes("//p"); 46 47 assertTrue("At least one <p>", pList.size() > 0); 48 } 49 50 protected void setUp() throws Exception { 53 super.setUp(); 54 document = getDocument("/xml/nitf/sample.xml"); 55 } 56 57 protected Document transform(String xsl) throws Exception { 58 assertTrue("Document is not null", document != null); 59 60 TransformerFactory factory = TransformerFactory.newInstance(); 62 Transformer transformer = factory.newTransformer(new StreamSource ( 63 getFile(xsl))); 64 65 Source source = new DocumentSource(document); 68 DocumentResult result = new DocumentResult(); 69 transformer.transform(source, result); 70 71 return result.getDocument(); 72 } 73 } 74 75 111 | Popular Tags |