java - eXist-db 从同一集合的许多 XML 中获取数据

标签 java groovy xquery exist-db openehr

我刚开始使用 eXist-db。我正在使用 Java/Groovy 尝试(没有运气)从我创建的集合中获取数据:/db/apps/compositions

/db/apps/compositions 中有几个与此类似的 XML 文档:

<version xmlns="http://schemas.openehr.org/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ORIGINAL_VERSION">
  ...
  <data xsi:type="COMPOSITION" archetype_node_id="openEHR-EHR-COMPOSITION.signos.v1">
    <name>
      <value>xxxxx</value>
    </name>
    ...
  </data>
</version>

我在客户端代码中使用 XQJ API。我尝试调整示例代码(来自 http://en.wikipedia.org/wiki/XQuery_API_for_Javahttp://xqj.net/exist/ ):

XQDataSource xqs = new ExistXQDataSource();
xqs.setProperty("serverName", "localhost");
xqs.setProperty("port", "8080");

XQConnection conn = xqs.getConnection("user","pass");

XQExpression expr = conn.createExpression();

XQResultSequence result = expr.executeQuery(
  "for $n in fn:collection('/db/apps/compositions')//data " +
  "return fn:data($n/name/value)"); // execute an XQuery expression

// Process the result sequence iteratively
while (result.next()) {
  // Print the current item in the sequence
  System.out.println("Product name: " + result.getItemAsString(null));
}

// Free all resources created by the connection
conn.close();

我希望从 /db/apps/compositions 集合中的所有 XML 文档中获取 xxxxx 文本,但我没有得到任何结果,也没有抛出任何异常。

有什么想法吗?

非常感谢!

顺便说一句:我尝试寻找实现 java 客户端的其他方法,但找不到针对初学者的明确指南或教程。

最佳答案

你遇到的问题都是关于命名空间的;您的元素位于默认命名空间中,因此您需要在查询中定义该命名空间。

xquery version "3.0";

declare default element namespace "http://schemas.openehr.org/v1";

for $n in fn:collection('/db/apps/compositions')//data

return fn:data($n/name/value)

阅读更多内容,例如tech wiki

一般来说,我建议在优秀的 eXide 中测试查询。先使用 IDE,然后再将它们合并到代码中。 IDE 为您提供有关查询结果的快速反馈,以便您可以尝试一下您的查询。

注意书写

*:data

可能会减慢大型数据集的查询速度。

关于java - eXist-db 从同一集合的许多 XML 中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27099672/

相关文章:

java - 保证并发 Web 服务调用之间数据一致性的最佳执行方法?

java - Spring Boot JPA hibernate : No identifier specified for entity error even when @Id is present in the entity class

java - 我怎样才能拦截从 Java 到 Groovy 的调用——或者很容易地模拟它

xml - 从 XML 返回元素的第一个属性

sql - 返回xdmp :sql() values in CSV format XQuery

java - 在 Java 中分配给这个的解决方法?

java - 无状态页面上的signoutForm

android - 需要帮助设置 RoboSpock

google-app-engine - grails 1.3.1执行脚本GenerateViews时出错:

xpath - 如何从 xml 中删除命名空间