LinkedIn

Wednesday, September 8, 2010

Custom XPath in OSB 11g

There is already a documentation available for this cool new feature in OSB 11g to create custom XPath functions that can be used from the OEPE IDE or even from the Service bus console.

For the ones who have a touch time to understand the Oracle documentation, here is a brief tutorial.

Please refer to the Oracle Document here for the detailed steps:

http://download.oracle.com/docs/cd/E14571_01/doc.1111/e15866/custom_xpath.htm#CBADFJEG

I would start of by creating my own xml for my own custom function. I would name it custom-osb-xpath.xml with a corresponding .properties file associated with it. Here are the contents:

custom-osb-xpath.xml

<?xml version="1.0" encoding="UTF-8"?>
<xpf:xpathFunctions xmlns:xpf="http://www.bea.com/wli/sb/xpath/config">
<xpf:category id="%CUSTOM_FUNCTIONS%">
<xpf:function>
<xpf:name>concatStrings</xpf:name>
<xpf:comment>%FUNC_CONC_COMMENT%</xpf:comment>
<xpf:namespaceURI>http://www.dell.com/alsb/custom/xpath/xpath-functions</xpf:namespaceURI>
<xpf:className>com.caterpillar.alsb.custom.xpath.StringConcat</xpf:className>
<xpf:method>java.lang.String concatString(java.lang.String, java.lang.String)</xpf:method>
<xpf:isDeterministic>false</xpf:isDeterministic>
<xpf:scope>Pipeline</xpf:scope>
<xpf:scope>SplitJoin</xpf:scope>
</xpf:function>
</xpf:category>
</xpf:xpathFunctions>

custom-osb-xpath.properties

%CUSTOM_FUNCTIONS%=Custom Service Bus Functions
%FUNC_CONC_COMMENT%=Returns a concatenated string based on the input strings. E.g. fn-bea:concatStrings($string1 as xs:string, $string2 as xs:string )


Place these files in OSB_ORACLE_HOME/config/xpath-functions directory of the OSB installation.

Now we would create a sample java class to concatenate two strings

package com.caterpillar.alsb.custom.xpath;

import java.lang.String;

public class StringConcat {

public static java.lang.String concatString(String stringa, String stringb)
{
String concatedStr= new String(stringa);
return concatedStr.concat(stringb);
}
}

Thats all we need. We are good to go now. Restart the OSB server or the Eclipse IDE. On booting them up we would start to see these custom functions added.

Here is a sample Xquery i created to use this custom XPath funtion

declare namespace xf = "http://tempuri.org/CommonServices/Common/Monitoring/XQ/ConcatStrings/";

declare function xf:ConcatStrings($string1 as xs:string,
$string2 as xs:string)
as xs:string {
xpat8i:concatStrings($string1,$string2)
};

declare variable $string1 as xs:string external;
declare variable $string2 as xs:string external;

xf:ConcatStrings($string1,
$string2)


Simple but so useful!

Tuesday, September 7, 2010

Xquery Enhancements using XQSE

Well, I dont need to reiterate the usefulness of Xquery when it comes to OSB development. This is one of the few good XML technologies which i find amazing for XML processing and transformations but it is equally neglected as well. It needs to evolve as a programming language. As a matter of fact, I have found it lacking of so many things that are so critical sometimes.

During one of my transformation exercises I was supposed to handle date conversions in Xquery. The problem with using Xquery was that if any of the date formats comes wrong this would lead to an error in my Xquery and it will be aborted. However in my case what i needed to do was simply send a null/empty value in date field if the conversion failed for any reason. Miserable situation if we have only Xquery.

BEA came up with an extension for XQuery called XQSE (Read XQuery Scripting Extension) to use more programmatic blocks in Xquery. Here is an example

declare xqse function xf:CreateDateTimeFromString($DateTime as xs:string?)
as xs:dateTime? {

try{
if(fn:string-length($DateTime)>8) then
return value  ( xs:dateTime(fn:concat (fn:substring($DateTime,1,4),'-',fn:substring($DateTime,5,2),'-', fn:substring($DateTime,7,2),'T',
fn:substring($DateTime,9,2),':',fn:substring($DateTime,11,2),':',fn:substring($DateTime,13,2)))) ;

return value  (xs:dateTime(fn:concat(fn:substring($DateTime,1,4),'-',fn:substring($DateTime,5,2),'-', fn:substring($DateTime,7,2),'T','00:00:00')));

}catch (* into $err, $msg) {
return value  () ;
};
};

The above xqse function allows us to surround our xquery with a try catch block. This is extremely useful for the case i explained above.