eXist-db getting data from many XMLs of the same collection

777 views Asked by At

I'm new to using eXist-db. Using Java/Groovy I am trying (with no luck) to get data from a collection I created: /db/apps/compositions.

In /db/apps/compositions are a couple of XML documents that look similar to this:

<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>

I am using the XQJ API in my client side code. I tried to adapt the example code (from http://en.wikipedia.org/wiki/XQuery_API_for_Java and 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();

I expected to get the xxxxx texts from all the XML Document in the /db/apps/compositions collection but I get no results and no exceptions are thrown.

Any ideas?

Thanks a lot!

BTW: I tried to find other ways of implementing a java client, but couldn't find a clear guideline or tutorial for beginners.

1

There are 1 answers

3
DiZzZz On

The problem you have is all about namespaces; your element is in default namespace so you need to define that namespace in your query.

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)

read more in e.g. the tech wiki

In general I'd recommend to test queries in the the excellent eXide IDE first before merging them into code. The IDE provides you fast feedback on query results so you can play a bit with your queries.

Note that writing

*:data

might slowdown queries on large datasets.