我不是 XQuery 专家。只是知道足以应付。最近,我将我的 Xquery 执行代码从 Saxon 8.4 迁移到了 9.9.1.2。所以我对 XQ 文件的执行方式做了一些改变。代码没有错误,但在运行时,我得到一个异常:
java.lang.IllegalArgumentException:提供的节点必须使用相同或兼容的配置构建
我为运行 XQ 文件而修改的代码如下所示:
// Prepare the environment for Saxon
SaxonErrorListener listener = new SaxonErrorListener();
listener.setLogger(new StandardLogger(new PrintStream(errors, true, "UTF-8")));
getSaxonConfig().setErrorListener(listener);
StaticQueryContext staticContext = new StaticQueryContext(getSaxonConfig());
Configuration configuration = new Configuration();
staticContext.setBaseURI("");
// Set up the dynamic context
DynamicQueryContext dynamicContext = new DynamicQueryContext(getSaxonConfig());
if ((xmlData != null) && (xmlData.length() > 0)) {
dynamicContext.setContextItem(configuration.buildDocumentTree(new StreamSource(new StringReader(xmlData))).getRootNode());
}
// If required use a (URI) uriResolver to handle xquery "doc" functions and module imports
if (uriResolver != null) {
dynamicContext.setURIResolver(uriResolver);
staticContext.setModuleURIResolver(uriResolver);
}
// If any external variables required, add them to the dynamic context
if (getExternalVariables().size() > 0) {
for (String varname : getExternalVariables().keySet()) {
StructuredQName structuredQName = new StructuredQName("", "", varname);
ObjectValue<Object> objectValue = new ObjectValue<Object>(getExternalVariables().get(varname));
dynamicContext.setParameter(structuredQName, objectValue);
}
}
// Prepare the XQuery
XQueryExpression exp;
exp = staticContext.compileQuery(xQuery);
// Run the XQuery
StringWriter out = new StringWriter();
exp.run(dynamicContext, new StreamResult(out), saxonProps);
// Collect the content
xqResult = out.toString();
引发错误的行是:
dynamicContext.setContextItem(configuration.buildDocumentTree(new StreamSource(new StringReader(xmlData))).getRootNode());
现在我用谷歌搜索了解决方案,但没有找到太多关于此的信息。XQ 文档也没有太多我可以学习的示例。任何帮助,将不胜感激。谢谢!