Friday, November 22, 2013

fn:tokenize() and fn:string-join() in OSB

In this post, I am trying to cover functionality of both fn:tokenize() and fn:string-join() XQuery functions of OSB.

I have a situation to deal with strings with comma separated values.

Situation :

Say there is a field "ID", in a database table which consists of values as comma separated strings.

Ex: 100,200,300,400,500


And this string needs to be passed to a query as a parameter. 

Query looks like this.


Select name,dept,location from mytable where id in('100','200',300',400','500')

Here, I need to convert the string coming from database table into a proper format.


String coming from the table   :   100,200,300,400,500
Desired Format                        :    '100','200',300',400','500'

Approach:

Say, the comma separated string is in a variable of OSB Proxy Service. And the variable is $varIDs.

fn:tokenize() :

fn: tokenize() splits the comma separated string and populates each string in the variable $id. In each iteration,capture every string using  an element. 


Now, use the function fn:tokenize() to split the string into parts by removing the commas. After splitting the string we need to handle each string. Here I am capturing each string as an element.

Inside the Xquery expression of an assign action writing an expression as in the screenshot.


Resultant XML is as follow.

<tokens>
<token>100</token>
<token>200</token>
<token>300</token>
<token>400</token>
<token>500</token>
<tokens>

Say this XML content is saved in a variable $varTokens.

Now we need to construct the final string that is passed to the SQL query.

fn:string-join() :

Following is the xquery expression.



Here, we iterate over each token id inside the fn:string-join() function. First, using the concat function a string is constructed like, '100'. Now each such string is joined with each other and each one is separated by a comma (This is the actual functionality of the fn:string-join() function).

Resultant string is '100','200','300','400','500'

Say, this string is saved in a variabie $varDelimIDs of the assign action. Now this variable can be used to construct the IN clause of the SQL statement.





Monday, October 21, 2013

XQuery expression to count certain element occurances in OSB

Some times we may need to count the number of elements in an XML document that start with specific characters.

Following examples shows the solution.

Ex:

We have XML as below. And this XML is in a variable say, $varXML

<ns0:Data_Structure Name="Group">
<ns0:Lookup Name="GroupReceiverID"></ns0:Lookup>
<ns0:Lookup Name="GroupSenderID"></ns0:Lookup>
<ns0:Lookup Name="GroupVersionNumber"></ns0:Lookup>
<ns0:Property Name="AckChildInvalid"></ns0:Property>
<ns0:Property Name="GroupAgencyCode"></ns0:Property>
<ns0:Property Name="GroupChildCount"></ns0:Property>
<ns0:Property Name="GroupControlNumber"></ns0:Property>
<ns0:Property Name="GroupDate"></ns0:Property>
<ns0:Property Name="GroupID"></ns0:Property>
<ns0:Property Name="GroupReceiverID"></ns0:Property>
<ns0:Property Name="GroupSenderID"></ns0:Property>
<ns0:Property Name="GroupTime"></ns0:Property>
<ns0:Property Name="GroupTrailerControlNumber"></ns0:Property>
<ns0:Property Name="GroupVersionNumber"></ns0:Property>
<ns0:Property Name="TPName"></ns0:Property>
<ns0:Data_Structure Name="Transaction">
<ns0:Lookup Name="ActionCode"></ns0:Lookup>
<ns0:Lookup Name="TransactionID"></ns0:Lookup>
<ns0:Property Name="ActionCode"></ns0:Property>
<ns0:Property Name="TPName"></ns0:Property>
<ns0:Property Name="TransactionChildCount"></ns0:Property>
<ns0:Property Name="TransactionControlNumber"></ns0:Property>
<ns0:Property Name="TransactionID"></ns0:Property>
<ns0:Property Name="TransactionImplementationReference"></ns0:Property>
<ns0:Property Name="TransactionTrailerControlNumber"></ns0:Property>
</ns0:Data_Structure>
</ns0:Data_Structure>

We need to find the number of elements that start with ns0:L . Here, we basically counting the number of ns0:Lookup elements in the entire XML data.

The XQuery expression to find the count would be as follow.

count($varXML//*[fn:starts-with(name(),'ns0:L')])

Use this expression inside any of the message processing actions of OSB.

Saturday, September 14, 2013

Subtracting Times in OSB using XQuery expression

If there is a need to calculate the Round Trip time of incoming and outgoing message in a Proxy Service of OSB we have an approach.

To subtract times you can use op:subtract-times() xquery function in OSB.

Example:

assing fn:current-time() to variable requestInTime.

Now to calculate the round trip time you can do the following in which we use the $requestInTime variable.

fn:substring-before(fn:substring(string(op:subtract-times(fn:current-time(),xs:time($requestInTime))), 3),'S')

Use above function in XQUERY expression in required action like assign, insert,replace.

Here, I've done some amount of formatting based on my requirement.
Essentially we need to use the op:subtract-times() function.

Hope this helps when you have a situation.

Friday, September 13, 2013

How to access an XML element with unknown name space prefix

We may come across a situation in which we are not sure of the namespace prefix of certain element. To retrieve such an element using XPATH we can use the wild character *

Example:

Say we have information of employees of an organization in an XML. XSD corresponding to employees.xml is employees.xsd. Also address details are separately defined in an XSD ,address.xsd and this XSD is referred in employees.xsd.

I am trying to access City of the first employee among multiple employees, and I do not know the namespace prefix of the Address element. Then I would write an Xpath as follow.


ns0:Employees/ns0:Employee[1]/*:Address/*:City/text()


This situation has helped me in writing an xquery expression in OSB.

However, it is always better to know what are all the namespaces and their prefixes that we are dealing with.