Database Reference
In-Depth Information
Example 2-1. XML file
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item
name=
"Bogus item"
>
This is a complete bogus item
</Item>
<Item
name=
"Funny item"
>
Ha, ha, very funny indeed!
</Item>
</Items>
Accessing data (and also scripts) is done through the eXist
REST interface
. To see it
in action, fire up your browser and visit
http://localhost:8080/exist/rest/apps/exist-
book/getting-started/xml-example.xml
.
The result is that you see exactly the file from
Example 2-1
. Not impressive, maybe,
but hey, this is only the beginning.
The REST interface allows you to directly query this file. For instance, assume you're
interested in the first item only. You can access it by adding a
_query
parameter:
http://localhost:8080/exist/rest/apps/exist-book/getting-started/
xml-example.xml?_query=//Item[1]
The result will be:
<exist:result
xmlns:exist=
"http://exist.sourceforge.net/NS/exist"
exist:hits=
"1"
exist:start=
"1"
exist:count=
"1"
>
<Item
name=
"Bogus item"
>
This is a complete bogus item
</Item>
</exist:result>
Because it's a query, eXist wraps the result in an
exist:result
element with addi‐
tional information in its attributes. There are other query parameters that will let you
limit the size of the result set and even retrieve the results block by block. More infor‐
mation about the REST interface can be found in
“Querying the Database Using
REST” on page 94
.
Hello XQuery
Of course, the main language when you are dealing with eXist is XQuery, which is
the
language to access XML databases. Put your XQuery script in a file (or database
document) with the extension
.xq.
Example 2-2
shows you a basic way to output
some XML.
Example 2-2. Basic XQuery code returning XML
xquery
version
"3.0"
;
let
$
msg
:=
'Hello XQuery'
return
<results
timestamp
=
"{
current-dateTime
()}"
>
<message>
{
$
msg
}
</message>
</results>
Search WWH ::
Custom Search