Wednesday, November 28, 2012

XQuery Short Tutorial - Part 1

XQuery is a query and function language that is used to do the query over the XML document.  It is widely used in SOA and integration environment where XQuery is used to transform the messages from one form to another.  Here it has the same functionality as XSLT.  From this perspective XQuery and XSLT look the same.   But after looking inside there are still many aspects that they are quite different.  For example XSLT itself is a XML document while XQuery itself has its own syntax and structure.   For someone who is adept in XSLT will feel bit not accustomed to the way of XQuery at first.  Here I would like to present a short XQuery tutorial for the people who have some experience with XML, XPath and XSLT to use XQuery confidently as quick as possible.

There are two parts of this tutorials,   To view the second part click on this url: XQuery Short Tutorial - Part 2

What does a XQuery look like?

First let us look at what a XQuery look like and what structure it has.  A XQuery is mainly made up of two parts: a prolog and a body.   The prolog part comes first in the query.  The prolog contains various declarations delimited by the semicolons.   The body part is the portion that be executed by XQuery engine to generate the output.

Example 1 
xquery version "1.0";
declare namespace tns = "";

The above example is one very simple valid XQuery.   The first line xquery version "1.0"; is XQuery declaration.   The second line is the prolog part of XQuery where the namespaces, global variables, functions and etc. are defined here.  Note that in each statement of declaration always ends with semi-colon ; .    The last line is the body part of XQuery.    This is the meat of XQuery.

Data model in XQuery
The data model of XQuery is based on two core components: Atomic value and node.

Atomic value is the simple data value without markup.  The examples of atomic values are xs:int, xs:dateTime, xs:Boolean and etc.
Node is referred as XML node which can be element, attribute, text or other XML constructs.

The below is one example of node in XQuery.
Example 2 

The general term for atomic value and node is called item.   Multiple items can make up a sequence.
In XQuery the sequence is represented as a list of items delimited by comma.

The below are two examples of the sequence in XQuery.  The first one is a sequence of atomic values and the second one a sequence of nodes.

Example 3
1,2, 3.5, ‘Hello World’

Example 4

How XQuery generates the output
We have seen what one XQuery looks like and the data model it uses.   Now we will see how XQuery generates the output.  Let us execute the first simple XQuery (Example 1) and see the output of the first XQuery example.

The output of example is shown as the below:


We can see from the above output comes from the body part of XQuery.  The body part of XQuery is the template of the output.  There are static portions and dynamic portions. When XQuery engine processes the body part it will output the static portions and generate the output for the dynamic portions or from function calls.  Static portion are the atomic type literals or node type literals.  Example 1 and example 5 show such static portions.  Example 5 is using atomic value literals and example 1 using node type literals.

Example 5

xquery version "1.0";
123, 345

The output of this XQuey is shown as:

123 345

The power of XQuery is mainly from the dynamic portions.   The dynamic portions are from the function calls (shown in example 6, 7, 8, 9, 10), global variables (in example 8 and 9), or a FLOWR (in example 10).  When XQuery find these dynamic portions it will do some computation first and then put the result into the output.

In the below example the body is a single function call,
Example 6

xquery version "1.0";
declare namespace math="";
declare function math:multiple($arg1 as xs:int, $arg2 as xs:int)
  as xs:int  {
   $arg1 * $arg2
math:multiple(2, 3)

The output will be:

 The below example will use two function calls in the body part.  Note that there is one comma between two calls.

Example 7
xquery version "1.0";
declare namespace math="";
declare function math:multiple($arg1 as xs:int, $arg2 as xs:int)
  as xs:int  {
            $arg1 * $arg2
math:multiple(2, 3),  math:multiple(3, 4)

 This will generate one sequence of integer values.
6 12

Example 8 shows that body part of XQuery is two global variables declared in the prolog part.  Here is also one comma between two variables.

Example 8
xquery version "1.0";
declare variable $displayedText1 as xs:string := 'Displayed Text 1';
declare variable $displayedText2 as xs:string := 'Displayed Text 2';

And the output is as the below:
Displayed Text 1 Displayed Text 2

Example 9 shows that body part of XQuery is one arithmetic expression using two global variables.
Example 9 
xquery version "1.0";
declare variable $d1  :=  2;
declare variable $d2  :=  3;

The output is the result of this arithmetic expression which is the product of two numbers.


Example 10 shows that body is using one FLOWR expression.

No comments:

Post a Comment