1 7 8 package org.dom4j.xpath; 9 10 import junit.textui.TestRunner; 11 12 import java.io.File ; 13 import java.util.List ; 14 15 import org.dom4j.AbstractTestCase; 16 import org.dom4j.DocumentHelper; 17 import org.dom4j.XPath; 18 import org.dom4j.io.SAXReader; 19 20 import org.jaxen.SimpleNamespaceContext; 21 22 28 public class PrefixTest extends AbstractTestCase { 29 protected static String [] paths = {"//xplt:anyElement", "//xpl:insertText", 30 "/Template/Application1/xpl:insertText", 31 "/Template/Application2/xpl:insertText"}; 32 33 public static void main(String [] args) { 34 TestRunner.run(PrefixTest.class); 35 } 36 37 public void testXPaths() throws Exception { 40 int size = paths.length; 41 42 for (int i = 0; i < size; i++) { 43 testXPath(paths[i]); 44 } 45 } 46 47 protected void testXPath(String xpathText) { 50 XPath xpath = DocumentHelper.createXPath(xpathText); 51 52 SimpleNamespaceContext context = new SimpleNamespaceContext(); 53 context.addNamespace("xplt", "www.xxxx.com"); 54 context.addNamespace("xpl", "www.xxxx.com"); 55 xpath.setNamespaceContext(context); 56 57 List list = xpath.selectNodes(document); 58 59 log("Searched path: " + xpathText + " found: " + list.size() 60 + " result(s)"); 61 62 assertTrue("Should have found at lest one result", list.size() > 0); 63 } 64 65 protected void setUp() throws Exception { 66 super.setUp(); 67 document = new SAXReader().read(new File ("xml/testNamespaces.xml")); 68 } 69 } 70 71 107 | Popular Tags |