XQuery 对命名空间声明的查询意外结束 [英] XQuery unexpected end of query on namespace declaration

查看:44
本文介绍了XQuery 对命名空间声明的查询意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行此查询:

I'm trying to execute this query:

declare variable $doc as xs:string external;
declare namespace type4="http:///de/tudarmstadt/ukp/dkpro/core/api/segmentation/type.ecore";
fn:doc($doc)//type4:Lemma/@value

在 BaseX java 驱动程序中.实际的代码片段如下所示:

within the BaseX java driver. The actual code snippet looks like this:

String queryString = "declare variable $doc as xs:string external; " +
        "declare namespace type4=\"http:///de/tudarmstadt/ukp/dkpro/core/api/segmentation/type.ecore\"; " +
        "fn:doc($doc)//type4:Lemma/@value";

Set<String> lemmata = new TreeSet<>();
try (ClientQuery query = this.clientSession.query(queryString))
{
    query.bind("$doc", this.getUriFromDocumentId(documentId));

    while (query.more())
    {
        String next = query.next();
        logger.info(next);
        lemmata.add(next);
    }

    return lemmata;
} catch (IOException e)
{
    e.printStackTrace();
    throw new QHException(e);
}

我收到了这个异常:

[XPST0003] Unexpected end of query: 'namespace type4...'

调用 query.more() 时.

我是否声明命名空间错误?java代码中的转义引号是否有错误?我不明白 xquery 从哪里得到查询的结尾.

Am I declaring the namespace wrong? Is there a mistake in the escaped quotes in the java code? I don't understand where xquery gets the end of query from.

命名空间也在我查询的 xml 文档中声明.

The namespace is also declared in the xml documents which I am querying.

this.getUriFromDocumentId(String documentId) 只是在数据库名称前面加上 uri,这样 uri 是完整的,并且实际上与我要查询的文档相匹配.在执行上面的代码片段之前,我检查了该文档是否存在.

this.getUriFromDocumentId(String documentId) just prepends the database name so that the uri is complete and actually matches the document I want to query. I check, that said document exists, before the code snippet above is executed.

推荐答案

XQuery 表达式的序言(在其中指定声明)是 分为两部分:命名空间声明必须在变量声明之前指定,因为前一个可能被后一个引用.

The prolog of an XQuery expression (in which you specify the declarations) is organized into two parts: Namespace declarations must be specified before variable declarations, as the first ones may be referenced by the latter ones.

实际上,这意味着您需要交换查询的前两行.

In practice, this means that you need to swap the first two lines of your query.

declare namespace type4="http:///de/tudarmstadt/ukp/dkpro/core/api/segmentation/type.ecore";
declare variable $doc as xs:string external;
fn:doc($doc)//type4:Lemma/@value

请注意,这是一个纯粹的语法问题,与 Java 驱动程序无关.我建议您使用 BaseX GUI 编写查询,并在查询运行后将结果查询移动到您的驱动程序代码中.

Please note that this is a purely syntactical issue, it has nothing to do with the Java driver. I recommend you to use the BaseX GUI for writing queries and move the resulting queries to your driver code once they are working.

这篇关于XQuery 对命名空间声明的查询意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆