0

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

4

1 回答 1

1

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

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

我看不出是什么getSaxonConfig(),但我的猜测是,如果你改变

Configuration configuration = new Configuration();

Configuration configuration = getSaxonConfig();

那么问题就会消失。

于 2019-11-25T11:12:33.323 回答