【问题标题】:XQuery Saxon Exception (java.lang.IllegalArgumentException)XQuery 撒克逊异常 (java.lang.IllegalArgumentException)
【发布时间】:2019-11-25 08:21:22
【问题描述】:

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

【问题讨论】:

  • 您至少调用了两次getSaxonConfig(),该函数是否确保您返回并使用相同的配置?至少错误消息表明您使用的是两个不同的。

标签: java xquery saxon xqj


【解决方案1】:

从 8.4 开始,您使用的 API 类和方法(例如 StaticQueryContext 和 DynamicQueryContext)不再是最佳实践,如果它们完全有效的话。 s9api 接口是在 9.1 左右引入的,更加实用和稳定。

但是,错误是因为您有多个 Saxon Configuration 对象。我无法确切地看到发生了什么,因为您没有向我们展示完整的图片,但是当必须已经有一个现有的 getSaxonConfig() 调用访问时创建一个 new Configuration() 看起来像个坏消息。

我看不到getSaxonConfig() 做了什么,但我猜如果你改变了

Configuration configuration = new Configuration();

到

Configuration configuration = getSaxonConfig();

那么问题就会消失。

【讨论】:

  • 对不起。获取 saxon config 只返回使用 Configuration configuration = new Configuration(); 初始化的 Configuration 对象
  • 您说我正在使用不再推荐的旧 API/类。关于如何使用 s9api 找到新东西并运行 XQ 有什么建议吗?此代码已有 6 或 7 年的历史。它原封不动地躺在那里。由于它稳定且有效,因此没有人不时打扰。现在由于迁移,所有这些都需要升级。
  • 嗯,简单的改变是避免创建第二个配置。但是如果您想使用 s9api API,那么文档位于 saxonica.com/documentation/index.html#!javadoc/…(从 Processor 类及其 newXQueryCompiler 方法开始),您可以在 saxon-resources 下载中下载示例,也可以在 www.saxon-resources 下载。 saxonica.com
  • 是的。双重配置对象导致了问题。与此同时,我正在寻找样品。我发现了这个:saxonica.com/html/documentation/using-xquery/api-query/…。它没有太多的示例代码,或者至少我找不到它。但我会给它一个红色的好去!感谢您的帮助!
  • 转到saxonica.com/download/download_page.xml 并向下滚动到“其他资源”。
猜你喜欢
  • 1970-01-01
  • 2016-01-25
  • 2013-05-20
  • 2021-12-22
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多