Database Reference
In-Depth Information
Example 3-1. The full code to get the play information
xquery
version
"3.0"
;
<plays>
{
for
$
resource
in
collection
(
"/db/apps/exist101/data"
)
return
<play
uri
=
"{
base-uri
(
$
resource
)}"
name
=
"{
util:unescape-uri
(
replace
(
base-uri
(
$
resource
),
".+/(.+)$"
,
"$1"
),
"UTF-8"
)}"
>
{
$
resource
/
PLAY
/
TITLE
/
text
()
}
</play>
}
</plays>
And, with some slight improvements (using variables to store repeating data), it
looks like
Example 3-2
.
Example 3-2. Improved version of the code that gets the play information
xquery
version
"3.0"
;
<plays>
{
let
$
data-collection
:=
"/db/apps/exist101/data"
for
$
resource
in
collection
(
$
data-collection
)
let
$
uri
:=
base-uri
(
$
resource
)
return
<play
uri
=
"{
$
uri
}"
name
=
"{
util:unescape-uri
(
replace
(
$
uri
,
".+/(.+)$"
,
"$1"
),
"UTF-8"
)}"
>
{
$
resource
/
PLAY
/
TITLE
/
text
()
}
</play>
}
</plays>
Don't forget to save your code. After that, you can also try it from the browser. Try
the URL
http://localhost:8080/exist/rest/db/apps/exist101/playlist.xq
. You should see
the following:
<plays>
<play
uri=
"/db/apps/exist101/data/hamlet.xml"
name=
"hamlet.xml"
>
The Tragedy of Hamlet, Prince of Denmark
</play>
<play
uri=
"/db/apps/exist101/data/macbeth.xml"
name=
"macbeth.xml"
>
The Tragedy of Macbeth
</play>
<play
uri=
"/db/apps/exist101/data/r%20and%20j.xml"
name=
"r and j.xml"
>
The Tragedy of Romeo and Juliet
</play>
</plays>
Search WWH ::
Custom Search