In the new Oracle Service Bus 11g, Oracle gives us new functionality for the use of custom xpath functions.
These functions we eventually can re-use in for example the xslt or xquery transformations.
For this we need :
- 1. XML Configuration file (osb-custom.xml)
- 2. Property file (optional, osb-custom.properties)
- 3. Custom Function Class (jar package)
All these files need to be stored at the next location
[sourcecode language=”xml”]
[/sourcecode]
1. XML Configuration file
Create a new XML configuration file. I made a copy of the default file, osb-built-in.xml, edit that one, and change the settings.
[sourcecode language=”xml”]
[/sourcecode]
A few notes on this file.
- category, the name of the category how it will appear in the IDE or Console
- function:name,comments, the description of the function how it will appear in the IDE or Console
- namespaceURI, the namespace which will be imported in for example the xquery to identify the custom function
- className, the java implementation of our custom xpath function
- method, the name of the method which we will be using for the implentation of the function
- isDeterministic, A value of true or false declaring whether or not the function is deterministic, the XQuery standard recommends the function to be deterministic (see documentation for further information)
- scope, i assume the scope of the OSB flow in which the function can be used
2. Property file
%OSB_FUNCTIONS%=Service Bus Functions
%FUNC_CUSTOM_CONCAT_COMMENT%=Returns the concat of the first and the second parameter
Not much in here,just a few placeholders for the descriptions
3. Custom Function Class
Create a new Java project in your favorite IDE. Create a new package and java class. For this demo i created a custom concat function which will concat parameter1 and parameter2 and return the result.
[sourcecode language=”java”]
package nl.iteye.osb.custom.functions;
public class OsbUtils {
public OsbUtils() {
}
public static String customConcat(String firstParam, String secondParam) {
return firstParam + ” ” + secondParam;
}
}
[/sourcecode]
The method needs to be defined as static.
Compile the code and create a jar archive and store this at the location mentioned in the top of the article.
After this we should have 3 extra files in this directory :
- osb-custom.xml
- osb-custom.properties
- osb-custom.jar
Restart the server, so the Oracle Service Bus will load the custom classes we just created. Check the output of the console to see if don’t see any errors come by. Wrong configuration (wrong methodname,package, etc) will end up with a little stacktrace in here.
Testing
To test the custom xpath function, create a new Oracle Service Bus project in Eclipse.
Add new XML Schema
[sourcecode language=”xml”]
[/sourcecode]
Add new XQuery Transformation
In the list of expression functions we will see our custom function (customConcat)
[sourcecode language=”xml”]
(:: pragma bea:global-element-parameter parameter=”$input1″ element=”ns0:Input” location=”NewXMLSchema.xsd” ::)
(:: pragma bea:global-element-return element=”ns0:Output” location=”NewXMLSchema.xsd” ::)
declare namespace ns0 = “http://www.example.org/NewXMLSchema”;
declare namespace xf = “http://tempuri.org/OSB%20Project%201/InputToOutput/”;
declare function xf:InputToOutput($input1 as element(ns0:Input))
as element(ns0:Output) {
};
declare variable $input1 as element(ns0:Input) external;
xf:InputToOutput($input1)
[/sourcecode]
In the transformation i will map both the Name1 and Name2 to the input of the custom function, which will return the concat of both in output.
The easiest way to test the result is by running the XQuery on a connected server. Right mouseclick on the .xq file > Run As > Run on Server
The Oracle Service Bus Test Console will pop up.
Fill in the input data and execute.
And the result
test
29 Responses to “Oracle Service Bus 11g, Using Custom Xpath functions”
[…] Adres URL: Oracle Service Bus 11g, Using Custom Xpath functions « Oracle … […]
Fantastic article.
One question, why prefix is osbk3? Is possible to change this?
Thanks!
You could add your own prefix
declare namespace test1 = “http://nl.iteye/osb/custom/functions/OsbUtils”;
and then use : test1:customConcat()
Did it work for you ?
Yes, it works.
But i need to declare namespace in all XQuery Transformations. It’s possible declare global namespace? For example, fn-bea prefix it’s general for all XQuerys….
Thanks!
I have a similar problem. I can not set the custom prefix. Did you found a solution?
[…] Service Bus features a nice possibility to write custom XPath functions. Eric already blogged about it some time ago. Recently we stumbled upon an interesting question: How can NULL values of […]
Does it work for 10.3 as well?
No. It’s new in the 11g release.
Thank you for nice article.
Eric, thank you for great article!
To everybody struggling to set custom default prefix: Boys, this is Oracle! Its software usually covers 99% of your needs, but you just can’t implement that missing percent no matter effort you dedicate. 😉
Hi Eric,
I am trying to use 2 custom functions, one of it works perfect but the other is returning an Empty string. I know the function is able to read the value but its unable to bind the value in th Xquery on OSB. Please advise.
what is the datatype of the variable you’re trying to bind ? can you paste the code ?
I have checked it for many times but I still get the following error:
“unknown function (or number of arguments (6) is wrong)”
Any idea?
Can you give me your mail address so i can help you over there?
it’s easier for me when i can see the xml configuration and java files
My e-mail address is hero475@hotmail.com
Thx
Here is a more detailed description:
line 11, column 23: {err}XQ0017: “{http://temp.com/osb/custom/functions/OsbUtils}addCustomFunction”: unknown function (or number of arguments (2) is wrong)
I solved the problem by changing the jre version of the project to version 1.5
Even after i changed to JRE version 1.5, this error is not resolved
unknown function (or number of arguments (2) is
wrong)
Hi,Very good article.However Can I reuse the same xpath functions on my xsl transformation?(I already created the xslt and used some user defined xpath functions.Should I follow the config to reuse the functions and xslt in OSB?)
[…] of the readers of my blog post ‘Oracle Service Bus 11g, Using Custom Xpath Functions asked we can reuse the custom xpath functions in a xsl […]
Hi Aswini,
I made a short post on how to call the method from within a xsl transformation, see : http://www.xenta.nl/blog/2012/01/09/oracle-service-bus-using-custom-xpath-functions-in-xslt/
Hope it helps you a bit.
Hi Eric. Very good article.
If my custom function class needs external libraries, where should I place them?
I tried domain_home\lib and tried deploy them via console, but both didn’t work!
Thanks
Hi!
Can you try adding the jars to the server startup classpath and then see if you can use them in your custom functions jar?
Hi Eric. Thanks for tour reply.
Yes, I already did that and it works!
Thank you.
Hi,
how do u know the namespace prefix as osbK3? Did u configure it somewhere?
Cant understand this part. I can use declare in my xquery but how did u get this prefix
Thanks,
Vamsi
I think Oracle will generate it internally, so you don’t have any direct influence on it. When you drag/drop the function into your xquery you will get the namespace + prefix.
Hi,
Can someone please help me in doing this in weblogic integration 10.2 server.
Thanks in advance.
[…] http://www.xenta.nl/blog/2010/05/10/oracle-service-bus-11g-using-custom-xpath-functions/ […]