LinkedIn

Wednesday, December 1, 2010

Dynamic XQuery in Oracle Service Bus

Well routing to a dynamic Proxy Service or a Dynamic Business Service was easy in Oracle Service Bus and i think everyone using OSB would know about it.

Ever wondered how can your OSB code dynamically invoke a Xquery? Not much difficult. Here is an example to illustrate it.

Let us assume that you have three different XQ's and depending upon the content you would like one of them to be picked.

Create a RoutingRule.xml file and create an entry for these Xqueries. Here is how you can do that

--------------------------------------------------------------------------------------------------------------------------------------------
<Rules>

<Operations>
<Operation Name="Operation1">
<RoutingRules>
<RoutingRule>
<Transformation>
<XQ Name="Case1_XQ"/>
</Transformation>
</RoutingRule>
</RoutingRules>
</Operation>

<Operation Name="Operation2">
<RoutingRules>
<RoutingRule>
<Transformation>
<XQ Name="Case2_XQ"/>
</Transformation>
</RoutingRule>
</RoutingRules>
</Operation>

<Operation Name="Operation3">
<RoutingRules>
<RoutingRule>
<Transformation>
<XQ Name="Case3_XQ"/>
</Transformation>
</RoutingRule>
</RoutingRules>
</Operation>

<RoutingServices>
<RoutingXQ Name="Case1_XQ">
<Address>#ProjectName/#FolderName1/#XqueryName1</Address>
</RoutingXQ>
<RoutingXQ Name="Case2_XQ">
<Address>#ProjectName/#FolderName2/#XqueryName2</Address>
</RoutingXQ>
<RoutingXQ Name="Case3_XQ">
<Address>#ProjectName/#FolderName3/#XqueryName3</Address>
</RoutingXQ>
</RoutingServices>

</Rules>

--------------------------------------------------------------------------------------------------------------------------------------------

Now assign this XML file to a variable in OSB. Let us call it routingRules. Let us also suppose that we would like to do a tranformation based on operations. The input to each of the XQ's is same but depending upon the operation the transformtion would vary.

Write a Xquery to get the source XQ based on the operation and routingRules variable. Here is a sample.

--------------------------------------------------------------------------------------------------------------------------------------------

xquery version "1.0" encoding "Cp1252";
(:: pragma  parameter="$Rules" type="xs:anyType" ::)
(:: pragma  parameter="$Operation" type="xs:anyType" ::)
(:: pragma  type="xs:string" ::)
declare namespace xf = "http://tempuri.org/Examples/GetRoutingXQ";
declare function xf:getServicesXQ($Rules as element(*), $Operation as xs:string)
as xs:string {
let $Result := for $Rule in $Rules//Operations/Operation[@Name= $Operation]/RoutingRules/*
let $TransXQOperation:= $Rule/Transformation/XQ/@Name
for $RoutingAddress in $Rules//RoutingServices/RoutingXQ[@Name = $TransXQOperation]/Address
return $RoutingAddress
return data($Result)
};
declare variable $Rules as element(*) external;
declare variable $Operation as xs:string external;
xf:getServicesXQ($Rules,$Operation)

--------------------------------------------------------------------------------------------------------------------------------------------

Assign this Xquery by passing the Rules XML and the operation to a variable, say "dynaXQ".

Now create a normal assign Action in the OSB and click on Dynamic Xquery. Configure the Dynamic Xquery as shown below:



where $variable1 and $variable2 are inputs required for the actual Xquery.

So go ahead and add more dynamics to your OSB development.

No comments:

Post a Comment