我刚开始使用 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_Java 和 http://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/