|
|
|
||||||||
![]() |
|
|
![]() |
| Purpose |
This tutorial aims to help beginners to work with DRB APIŽ using
XQuery especially on the command line. Note: The presented samples are ordered by increasing complexity. |
|---|---|
| Target audience | Beginner |
| Required knowledge |
Before you continue you should have a basic understanding of the following:
|
| What you will learn in this tutorial | In this tutorial you will learn how to use the DRB APIŽ XQuery engine on command line. |
You may find usefull to config your environnment by using an alias,
so that the command of this tutorial appears simpler.
The following definition is for Bash
shell:
alias drb='java -jar [DRB_INSTALLATION_HOME]/lib/java/drb-[M]-[m]-[t].jar'
drb --version
May returns something like:
Data Request Broker - DRB API - 2-3-rc-2 (GAEL Consultant)
drb --help
drb --query '2000 + 9'
May return:
2009
The math: namespace is used for mathematical function available in the XQuery language.
For example computing a cosinus value:
drb --query 'math:cos(45)'
May return:
0.5253219888177297
Another valuable function is math:random() which
return a random value from 0 to 1.
drb --query 'math:random()'
May return:
0.30315802310561635
drb --help xql
Having the hotel.xml
:
<hotel>
<name>Hotel du Parc</name>
<address>
<street>7 place de la Concorde</street>
<zipcode>75008</zipcode>
<city>Paris</city>
<country>France</country>
</address>
<phone>+33 1 4280 5000</phone>
<fax>+33 1 4280 5001</fax>
<rooms>
<room>
<name>Royal Suite</name>
<price>99</price>
</room>
<room>
<name>Standard Suite</name>
<price>45</price>
</room>
</rooms>
</hotel>
You open the file with the command:
drb --query 'hotel.xml'
May return:
<hotel.xml directory="false" size="474" modified="16-MAR-2009 15:40:16.000000" readable="true" writable="true" hidden="false">
<hotel>
<name>Hotel du Parc</name>
<address>
<street>7 place de la Concorde</street>
<zipcode>75008</zipcode>
<city>Paris</city>
<country>France</country>
</address>
<phone>+33 1 4280 5000</phone>
<fax>+33 1 4280 5001</fax>
<rooms>
<room>
<name>Royal Suite</name>
<price>99</price>
</room>
<room>
<name>Standard Suite</name>
<price>45</price>
</room>
</rooms>
</hotel>
</hotel.xml>
The command for retrieving the hotel name node :
drb --query 'hotel.xml/hotel/name'
May return:
<name>Hotel du Parc</name>
The command for retrieving the hotel name value :
drb --query 'fn:data(hotel.xml/hotel/name)'
May return:
Hotel du Parc
The predicate (introduce with the symbols []) is
used for selecting a specific node. For example in case of several node with the
same name we can select the second one like in the following command:
drb --query 'hotel.xml/hotel/rooms/room[2]'
May return:
<room> <name>Standard Suite</name> <price>45</price> </room>
We can also retrieve node regarding some test on values. In the following example we select all room nodes that have a price node value strictly superior to 50.0
drb --query 'hotel.xml/hotel/rooms/room[xs:float(price) > xs:float(50.0)]'
May return:
<room> <name>Royal Suite</name> <price>99</price> </room>
Take care that the retrieve node value has to be converted
to the appropriate type
(here xs:integer)
for the computation to be possible:
drb --query 'xs:integer(fn:data(hotel.xml/hotel/rooms/room[2]/price)) - 10'
May return:
35
The query has to be placed between { }
:
drb --query '<root><a>{ math:random() }</a></root>'
May return:
<root> <a>0.047975992783876764</a> </root>
drb --query '<root><a>{ fn:data(hotel.xml/hotel/name) }</a></root>'
May return:
<root> <a>Hotel du Parc</a> </root>
drb --query '<root><a>{ hotel.xml/hotel/name }</a></root>'
May return:
<root>
<a>
<name>Hotel du Parc</name>
</a>
</root>
drb --query 'if (5 < 10) then "Five is less than ten." else "Five is not less than ten!!! Arrrggggg..."'
May return:
Five is less than ten.
drb --query 'for $i in 1 to 5 return fn:concat("iteration nb#", $i)'
May return:
iteration nb#1, iteration nb#2, iteration nb#3, iteration nb#4, iteration nb#5
The purpose of the redirection is for example to save the result of your query in a file.
Caution: This action depends on your environmment.
In Bash shell you can use the following command (note the
>
symbol):
drb --query '<root><a>{ math:random() }</a></root>' > output.xml
If you list the content of the file output.xml:
<root> <a>0.21997404015814814</a> </root>
A query script is a file containing XQuery instructions.
Having the following script file (script_random.xql
):
xquery version "1.0";
fn:concat("Hello, here is a random number: ", math:random())
You can execute it using the --query-file
option:
drb --query-file script_random.xql
May return:
Hello, here is a random number: 0.22337426002117922