XQuery Saxon异常(java.lang.IllegalArgumentException)
我不是XQuery专家。我知道的还不够多。最近,我将我的Xquery执行代码从Saxon 8.4迁移到了9.9.1.2。所以我对XQ文件的执行方式做了一些更改。代码没有错误,但在运行时,我遇到一个异常: java.lang.IllegalArgumentException:提供的节点必须使用 相同或兼容的配置 我为运行XQ文件而修改的代码如下所示:XQuery Saxon异常(java.lang.IllegalArgumentException),java,xquery,saxon,xqj,Java,Xquery,Saxon,Xqj,我不是XQuery专家。我知道的还不够多。最近,我将我的Xquery执行代码从Saxon 8.4迁移到了9.9.1.2。所以我对XQ文件的执行方式做了一些更改。代码没有错误,但在运行时,我遇到一个异常: java.lang.IllegalArgumentException:提供的节点必须使用 相同或兼容的配置 我为运行XQ文件而修改的代码如下所示: // Prepare the environment for Saxon SaxonErrorListener listener =
// 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();
现在我在谷歌上搜索解决方案,但没有找到太多关于这个的信息。XQ文档中也没有太多我可以学习的例子。任何帮助都将不胜感激。谢谢 从8.4开始,您使用的API类和方法(如
StaticQueryContext
和DynamicQueryContext
)如果仍然有效,则不再是最佳实践。s9api接口是在9.1左右引入的,它更易使用,也更稳定
但是,错误是因为您有多个SaxonConfiguration
对象。我看不出到底发生了什么,因为您还没有向我们展示完整的情况,但是创建一个新配置()
,而getSaxonConfig()
访问调用必须已经存在一个配置,这看起来是个坏消息
我看不出getSaxonConfig()
做了什么,但我猜如果您更改
Configuration configuration = new Configuration();
到
然后问题就会消失。您至少调用了两次
getSaxonConfig()
,该函数是否确保返回并使用相同的配置?至少错误信息表明您正在使用两个不同的。啊,对不起。Get saxon config只返回使用Configuration Configuration=new Configuration()初始化的配置对象代码>您说我使用的是不再推荐的旧API/类。关于如何找到新东西并使用s9api运行XQ有什么建议吗?此代码已存在6或7年以上。它原封不动地躺在那里。因为它是稳定和工作,没有人费心去看不时。现在由于迁移,所有这些都需要升级。好吧,简单的更改是避免创建第二个配置。但是,如果您想使用s9api,那么文档位于(从处理器
类及其新的XQueryCompiler
方法开始),您可以在saxon资源下载中下载示例,也可以在www.saxonica.com下载。双重配置对象导致了问题。与此同时,我在寻找样本。我发现了这个:。它没有太多的示例代码,或者至少我找不到它。但我会给它一个红色的好去!谢谢你的帮助!转到并向下滚动至“其他资源”。
Configuration configuration = new Configuration();
Configuration configuration = getSaxonConfig();