📄 xpathtestbase.java
字号:
public void testDocumentFunctionContextExample() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/text.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/foo/bar/cheese[1]", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertValueOfXPath("3foo3", context, "concat(./@id,'foo',@id)"); assertValueOfXPath("3snoop3", context, "concat(./@id,document('xml/web.xml')/web-app/servlet[1]/servlet-name,./@id)"); } } public void testDocumentFunctionActual() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/message.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertValueOfXPath("Pruefgebiete", context, "/message/body/data/items/item[name/text()='parentinfo']/value"); assertValueOfXPath("Pruefgebiete", context, "document('xml/message.xml')/message/body/data/items/item[name/text()='parentinfo']/value"); } } /* test behaviour of AbsoluteLocationPath */ public void testAbsoluteLocationPaths() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/simple.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/root/a", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertValueOfXPath("ab", context, "concat( ., /root/b )"); assertValueOfXPath("ba", context, "concat( ../b, . )"); assertValueOfXPath("ba", context, "concat( /root/b, . )"); assertValueOfXPath("db", context, "concat( /root/c/d, ../b )"); } } /* test the translate() function */ public void testTranslateFunction() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/simple.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertValueOfXPath("", context, "translate( '', '', '' )"); assertValueOfXPath("abcd", context, "translate( 'abcd', '', '' )"); assertValueOfXPath("abcd", context, "translate( 'abcd', 'abcd', 'abcd' )"); assertValueOfXPath("abcd", context, "translate( 'abcd', 'dcba', 'dcba' )"); assertValueOfXPath("dcba", context, "translate( 'abcd', 'abcd', 'dcba' )"); assertValueOfXPath("ab", context, "translate( 'abcd', 'abcd', 'ab' )"); assertValueOfXPath("cd", context, "translate( 'abcd', 'cdab', 'cd' )"); assertValueOfXPath("xy", context, "translate( 'abcd', 'acbd', 'xy' )"); assertValueOfXPath("abcd", context, "translate( 'abcd', 'abcdb', 'abcdb' )"); assertValueOfXPath("abcd", context, "translate( 'abcd', 'abcd', 'abcdb' )"); } } public void testSubstringFunction() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/simple.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertValueOfXPath("234", context, "substring('12345', 1.5, 2.6)"); assertValueOfXPath("12", context, "substring('12345', 0, 3)"); assertValueOfXPath("", context, "substring('12345', 0 div 0, 3)"); assertValueOfXPath("", context, "substring('12345', 1, 0 div 0)"); assertValueOfXPath("12345", context, "substring('12345', -42, 1 div 0)"); assertValueOfXPath("", context, "substring('12345', -1 div 0, 1 div 0)"); assertValueOfXPath("345", context, "substring('12345', 3)"); assertValueOfXPath("12345", context, "substring('12345',1,15)"); } } /* Some tests for the normalize-space() function */ public void testNormalizeSpaceFunction() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/simple.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertValueOfXPath("abc", context, "normalize-space(' abc ')"); assertValueOfXPath("a b c", context, "normalize-space(' a b c ')"); assertValueOfXPath("a b c", context, "normalize-space(' a \n b \n c')"); /* Next test case addresses issue JAXEN-22 */ assertValueOfXPath("", context, "normalize-space(' ')"); /* Next test case addresses issue JAXEN-29 */ assertValueOfXPath("", context, "normalize-space('')"); } } /* test cases for String extension functions */ public void testStringExtensionFunctions() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/web.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/web-app/servlet[1]", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertValueOfXPath("SNOOPSERVLET", context, "upper-case( servlet-class )"); assertValueOfXPath("snoopservlet", context, "lower-case( servlet-class )"); assertValueOfXPath("SNOOPSERVLET", context, "upper-case( servlet-class, 'fr' )"); assertValueOfXPath("SNOOPSERVLET", context, "upper-case( servlet-class, 'fr-CA' )"); assertValueOfXPath("SNOOPSERVLET", context, "upper-case( servlet-class, 'es-ES-Traditional_WIN' )"); assertValueOfXPath("true", context, "ends-with( servlet-class, 'Servlet' )"); assertValueOfXPath("false", context, "ends-with( servlet-class, 'S' )"); } } /* test cases for the lang() function */ public void testLangFunction() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/lang.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertCountXPath(0, context, "/e1/e2[lang('hr')]"); assertCountXPath(1, context, "/e1/e2/e3[lang('en')]"); assertCountXPath(1, context, "/e1/e2/e3[lang('en-US')]"); assertCountXPath(0, context, "/e1/e2/e3[lang('en-GB')]"); assertCountXPath(2, context, "/e1/e2/e3[lang('hu')]"); assertCountXPath(0, context, "/e1/e2/e3[lang('hu-HU')]"); assertCountXPath(1, context, "/e1/e2/e3[lang('es')]"); assertCountXPath(0, context, "/e1/e2/e3[lang('es-BR')]"); } } /* test namespace */ public void testNamespacesAgain() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/namespaces.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); SimpleNamespaceContext nsContext = new SimpleNamespaceContext(); nsContext.addNamespace("alias", "http://fooNamespace/"); nsContext.addNamespace("bar", "http://barNamespace/"); nsContext.addNamespace("voo", "http://fooNamespace/"); nsContext.addNamespace("foo", "http://fooNamespace/"); getContextSupport().setNamespaceContext(nsContext); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertCountXPath(1, context, "/*"); assertCountXPath(1, context, "/foo:a"); assertCountXPath(1, context, "/foo:a/b"); assertCountXPath(1, context, "/voo:a/b/c"); assertCountXPath(1, context, "/voo:a/bar:f"); assertCountXPath(1, context, "/*[namespace-uri()='http://fooNamespace/' and local-name()='a']"); assertCountXPath(1, context, "/*[local-name()='a' and namespace-uri()='http://fooNamespace/']/*[local-name()='x' and namespace-uri()='http://fooNamespace/']"); assertCountXPath(1, context, "/*[local-name()='a' and namespace-uri()='http://fooNamespace/']/*[local-name()='x' and namespace-uri()='http://fooNamespace/']/*[local-name()='y' and namespace-uri()='http://fooNamespace/']"); } } /* the prefix here and in the document have no relation; it's their namespace-uri binding that counts */ public void testPrefixDoesntMatter() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/namespaces.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); SimpleNamespaceContext nsContext = new SimpleNamespaceContext(); nsContext.addNamespace("foo", "http://somethingElse/"); getContextSupport().setNamespaceContext(nsContext); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertCountXPath(0, context, "/foo:a/b/c"); } } /* Jaxen-67, affects Jelly and Maven */ public void testCDATASectionsAreIncludedInTextNodes() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/cdata.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextPath = new BaseXPath("/p/text()", nav); log("Initial Context :: " + contextPath); List list = contextPath.selectNodes(document); // Depending on the object model, there can be anywhere from // 1 to 3 nodes returned here. StringBuffer buffer = new StringBuffer(10); Iterator iterator = list.iterator(); while (iterator.hasNext()) { buffer.append(StringFunction.evaluate(iterator.next(), nav)); } assertEquals("awhateverb", buffer.toString()); } public void testNamespaces() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/namespaces.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); SimpleNamespaceContext nsContext = new SimpleNamespaceContext(); nsContext.addNamespace("alias", "http://fooNamespace/"); nsContext.addNamespace("bar", "http://barNamespace/"); nsContext.addNamespace("foo", "http://fooNamespace/"); getContextSupport().setNamespaceContext(nsContext); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertValueOfXPath("Hello", context, "/foo:a/b/c"); assertValueOfXPath("Hey", context, "/foo:a/foo:d/foo:e"); assertValueOfXPath("Hey3", context, "/foo:a/alias:x/alias:y"); assertValueOfXPath("Hey3", context, "/foo:a/foo:x/foo:y"); assertValueOfXPath("Hey3", context, "/*[local-name()='a' and namespace-uri()='http://fooNamespace/']/*[local-name()='x' and namespace-uri()='http://fooNamespace/']/*[local-name()='y' and namespace-uri()='http://fooNamespace/']"); } } public void testNoNamespace() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/defaultNamespace.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); /* NOTE: /a/b/c selects elements in no namespace only! */ assertCountXPath(0, context, "/a/b/c"); /* The following test uses an unbound prefix 'x' and should throw an exception. Addresses issue JAXEN-18. Turns out this isn't really tested as the test didn't fail when the exception wasn't thrown. <test select="/x:a/x:b/x:c" count="0" exception="true"/> */ } } public void testNamespaceResolution() throws JaxenExce
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -