名前空間を含む XML に対して xpath クエリを実行しようとしています。残念ながら、xpath クエリまたは XML の名前空間を理解できないため、かなり恐ろしいエラー メッセージが表示されます。
サンプルの xpath クエリは次のとおりです。//b:menuBarItem[@label='Risk']/@id
JOOX (1.1.0)の名前空間を、組み込みの Java ライブラリを使用してここで行った方法と同様の方法で構成できますか?
private static NamespaceContext createXPathNameSpaceContext( final Map<String, String> namespaces ) {
NamespaceContext nsContext = new NamespaceContext() {
public Iterator<String> getPrefixes( String namespaceURI ) {
return null;
}
public String getPrefix( String namespaceURI ) {
return null;
}
public String getNamespaceURI( String prefix ) {
if ( namespaces.containsKey( prefix ) ) {
return namespaces.get( prefix );
} else {
return XMLConstants.NULL_NS_URI;
}
}
};
return nsContext;
}
次のコードで JOOX を呼び出しています。
import static org.joox.JOOX.$;
private void extractResponseData( Document responseXML, String xpath ) {
String match = $( responseXML ).xpath( xpath ).get( 0 ).getNodeValue();
// .. etc..
}
際立っているのは、名前空間を登録していないことですが、方法がわかりませんでした。私はかなり近視眼的で、API の何かを見落としているのではないかと思います。JOOX から取得しているスタック トレースは次のとおりです。
java.lang.RuntimeException: javax.xml.xpath.XPathExpressionException
at org.joox.Impl.xpath(Impl.java:494)
at org.joox.Impl.xpath(Impl.java:467)
at org.joox.Impl.xpath(Impl.java:93)
at end2end.http.HttpAction$XML.extractResponseData(HttpAction.java:181)
at end2end.http.XMLPostAction.extractResponseData(XMLPostAction.java:26)
at end2end.http.HttpAction.perform(HttpAction.java:58)
at end2end.TestXML$UserAction.fire(TestXML.java:176)
at end2end.TestXML$FireAction.fire(TestXML.java:148)
at end2end.TestXML.multipleUsers(TestXML.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:702)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:336)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:65)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:88)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
at org.testng.TestNG.run(TestNG.java:1030)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:215)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: javax.xml.xpath.XPathExpressionException
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:402)
at org.joox.Impl.xpath(Impl.java:486)
at org.joox.Impl.xpath(Impl.java:467)
at org.joox.Impl.xpath(Impl.java:93)
at end2end.http.HttpAction$XML.extractResponseData(HttpAction.java:181)
at end2end.http.XMLPostAction.extractResponseData(XMLPostAction.java:27)
at end2end.http.HttpAction.perform(HttpAction.java:58)
at end2end.TestXML$UserAction.fire(TestXML.java:176)
at end2end.TestXML$FireAction.fire(TestXML.java:148)
at end2end.TestXML.multipleUsers(TestXML.java:120)
... 24 more
Caused by: com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace:
at com.sun.org.apache.xpath.internal.compiler.XPathParser.errorForDOM3(XPathParser.java:653)
at com.sun.org.apache.xpath.internal.compiler.Lexer.mapNSTokens(Lexer.java:638)
at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:265)
at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:96)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:110)
at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176)
at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:394)
at org.joox.Impl.xpath(Impl.java:486)
at org.joox.Impl.xpath(Impl.java:467)
at org.joox.Impl.xpath(Impl.java:93)
at end2end.http.HttpAction$XML.extractResponseData(HttpAction.java:181)
at end2end.http.XMLPostAction.extractResponseData(XMLPostAction.java:26)
at end2end.http.HttpAction.perform(HttpAction.java:58)
at end2end.TestXML$UserAction.fire(TestXML.java:176)
at end2end.TestXML$FireAction.fire(TestXML.java:148)
at end2end.TestXML.multipleUsers(TestXML.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:702)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1197)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
at org.testng.TestNG.run(TestNG.java:1030)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
... 1 more