Database Reference
In-Depth Information
Let's look at how we would send an XQuery to the REST Server API to retrieve the
names of all the people in the collection. Our XQuery might look like Example 13-5 .
Example 13-5. XQuery to retrieve names of all people in the collection
xquery version "1.0" ;
/ person / name
HTTP GET queries. To send the XQuery in Example 13-5 to the REST Server API using
the simple HTTP GET approach, we can ignore the XQuery version declaration, as
eXist will default to XQuery 1.0. However, as we are going to place the XQuery into
the _query parameter in the URL, we should first URL-encode the XQuery to escape
any URL-sensitive characters. If you are doing these operations from a programming
language, there is most likely a library function already available for URL encoding;
otherwise, if you are using cURL or sending the queries manually, you can use a sim‐
ple URL encoder like URL Encode/Decode .
Our URL-encoded XQuery becomes:
% 2 Fperson%2Fname
We can now send this XQuery to the REST Server API using the following cURL
command:
curl "http://localhost:8080/exist/rest/db/people?_query=%2Fperson%2Fname"
which could result in a response similar to:
<exist:result xmlns:exist= "http://exist.sourceforge.net/NS/exist"
exist:hits= "3" exist:start= "1" exist:count= "3" >
<name>
<first-name> John </first-name>
<family-name> Smith </family-name>
</name>
<name>
<first-name> George </first-name>
<family-name> Baker </family-name>
</name>
<name>
<first-name> Barbara </first-name>
<family-name> Jones </family-name>
</name>
</exist:result>
By default the REST Server API wraps the result of our XQuery in an exist:result
element; this provides us with a container for our data and some metadata about the
number of results found by the query ( exist:hits ) and the number of results imme‐
diately returned ( exist:start and exist:count ). In this case, we can see that the
Search WWH ::




Custom Search