XQUERY

Getting Started - XQuery Dev inside OSB Eclipse
Prerequisite

  • Should have installed OSB 
  • Basics understanding of middleware technology. 

Introduction :
We can use XQuery/XSL in OSB to do the message transformation. But XQuery is preferred over the XSL because of the  performance advantage. In this post I will show how to develop an xquery using the osb workshop eclipse.



XQuery – Introduction
XQuery is a light weight language which has got an xml like syntax. XQuery has 3 important parts as below..
  • Namespace declaration
  • Set of function with their implementations
  • Declaration of variables
  • Mentioning the function with which the execution begins.

Sample XQuery
Listing-1    xquery version "1.0" encoding "Cp1252";
Listing-2    (:: pragma  parameter="$anyType1" type="xs:anyType" ::)
Listing-3    (:: pragma  type="xs:anyType" ::)
Listing-4    declare namespace xf = "http://tempuri.org/OSB%20Project%203/sample/";
Listing-5    declare namespace ns1 = "http://tempuri.org/OSB%20Project%203/sample/inputaddress";
Listing-6    declare namespace ns2 = "http://tempuri.org/OSB%20Project%203/sample/outputaddress";
Listing-7
Listing-8    declare function xf:sample($requestDoc as element(*))
Listing-9                    as element(*)
Listing-10    {
Listing-11                <ns2:Address>
Listing-12                  <ns2:name>
Listing-13                  {
Listing-14                  $requestDoc/ns1:FirstName/text()
Listing-15                  }
Listing-16                  </ns2:name>
Listing-17                  <ns2:phone>
Listing-18                  {
Listing-19                  $requestDoc/ns1:PhoneNumber/text()
Listing-20                  }
Listing-21                  </ns2:phone>
Listing-22                </ns2:Address>
Listing-23    };
Listing-24
Listing-25    declare variable $requestDoc as element(*) external;
Listing-26
Listing-27    xf:sample($requestDoc)

Explanation
Listing 1 - declaration of xquery
Listing 2 to 3 - comments
Listing 4 to 6 - Namespace declarations
Listing 8 to 9 - This is the code snippet for the function 'sample' which takes 'requestDoc' which is of type element(*),that is the xml. It returns the type element(*) which is also an xml.
Listing 10 - shows the start of the xquery function
Listing 23 - shows the end of the xquery function
Listing 11 to 22 - the body of the function 'sample'

Here we are doing the transformation.  Whatever we write it in these lines will be the final output of the xquery transformation

Listing 14 or 19 - Here we are assigning the data obtained from request xml to the output response xml.
Listing 25 - declaring the request variable 'requestDoc' as 'external' as type 'element(*)'. So when we test this xquery from the OSB flow, we will bind the request xml with 'requestDoc' variable and we can transform the request xml to the response xml. 

Listing 27 - This is the last line of the xquery where the execution of the xquery begins. Suppose if we have multiple functions, whatever function is given in the last line of xquery will be the entry point of execution.

No comments:

Post a Comment