gpt4 book ai didi

java - XQuery Saxon 异常 (java.lang.IllegalArgumentException)

转载 作者:行者123 更新时间:2023-12-02 09:15:26 24 4
gpt4 key购买 nike

我不是 XQuery 专家。只要知道足够的事情就可以了。最近,我将 Xquery 执行代码从 Saxon 8.4 迁移到 9.9.1.2。所以我对 XQ 文件的执行方式做了一些改变。代码没有错误,但在运行时,我得到一个异常:

java.lang.IllegalArgumentException: Supplied node must be built using the same or a compatible Configuration

我修改来运行 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 文档也没有太多可供我学习的示例。任何帮助,将不胜感激。谢谢!

最佳答案

从 8.4 开始,您使用的 API 类和方法(例如 StaticQueryContextDynamicQueryContext)即使有效,也不再是最佳实践。 s9api接口(interface)是在9.1左右引入的,更好用、更稳定。

但是,该错误是因为您有多个 Saxon Configuration 对象。我无法确切地看到发生了什么,因为您没有向我们展示完整的情况,但是在 getSaxonConfig() 必须已经存在一个现有配置的情况下创建一个 new Configuration() 调用 access 看起来像是个坏消息。

我看不到 getSaxonConfig() 的作用,但我的猜测是,如果你改变

Configuration configuration = new Configuration();

Configuration configuration = getSaxonConfig();

那么问题就会消失。

关于java - XQuery Saxon 异常 (java.lang.IllegalArgumentException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59027608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com