0

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.

GavinBrelstaff
  • 3,016
  • 2
  • 21
  • 39
Pablo Pazos
  • 3,080
  • 29
  • 42
  • 1
    The `data` element belongs to the default namespace declared on `version`. You can either introduce this namespace in your code or wildcard it by writing `*:data`. – Jens Østergaard Petersen Nov 24 '14 at 09:07
  • 2
    If you are looking for Java client examples, the eXist book dedicates the entirety of `Chapter 13 - Integration` to looking at the options and writing clients in Java: http://shop.oreilly.com/product/0636920026525.do The book should be in print next month. However in the mean time the code examples from the integration chapter (including XQJ) are available here: https://github.com/eXist-book/book-code/tree/master/chapters/integration – adamretter Nov 24 '14 at 11:46

1 Answers1

4

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.

DiZzZz
  • 621
  • 3
  • 12
  • Now the query is is getting results, just have one more issue: The version/data element also has descendant nodes named "data" and also with a name/value element, and the query is getting the name of all the descendants. Is there a way of getting just the name/value of the first data element? BTW, this is my modified code in Groovy: https://github.com/ppazos/clinical-database-tests/blob/master/src/existdb/TestExistDB.groovy – Pablo Pazos Nov 24 '14 at 14:39
  • Please an an explicit XML example, I don't understand what you write, sorry; it could look like depending on your exact structure ` xquery version "3.0"; declare default element namespace "http://schemas.openehr.org/v1"; for $n in fn:collection('/db/apps/compositions')//data[position() = 1] return fn:data($n/name/value) ` – DiZzZz Nov 24 '14 at 15:10
  • Here you can find a full example: https://github.com/ppazos/clinical-database-tests/blob/master/compositions/composition_8741_0.xml As you will see, **version/data** element has descendant elements (children, grandchildren, ...) that are also named _data_ and have **name/value**. The query is returning all the **data/name/value** declared in the document, not just the **version/data/name/value** that I'm trying to get. – Pablo Pazos Nov 24 '14 at 18:43