Oracle Service Bus 11g, Using Custom Xpath functions

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. 1. XML Configuration file (osb-custom.xml)
  2. 2. Property file (optional, osb-custom.properties)
  3. 3. Custom Function Class (jar package)

All these files need to be stored at the next location

<OSB_HOME>/config/xpath-functions

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.

<?xml version="1.0" encoding="UTF-8"?>
<xpf:xpathFunctions xmlns:xpf="http://www.bea.com/wli/sb/xpath/config">
    <xpf:category id="%OSB_FUNCTIONS%">
        <xpf:function>
            <xpf:name>customConcat</xpf:name>
            <xpf:comment>%FUNC_CUSTOM_CONCAT_COMMENT%</xpf:comment>
            <xpf:namespaceURI>http://nl.iteye/osb/custom/functions/OsbUtils</xpf:namespaceURI>
            <xpf:className>nl.iteye.osb.custom.functions.OsbUtils</xpf:className>
            <xpf:method>java.lang.String customConcat(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>

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.

package nl.iteye.osb.custom.functions;

public class OsbUtils {
    public OsbUtils() {
    }

    public static String customConcat(String firstParam, String secondParam) {
        return firstParam + " " + secondParam;
     }
}

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

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/NewXMLSchema" xmlns:tns="http://www.example.org/NewXMLSchema" elementFormDefault="qualified">
    <element name="Input" type="tns:InputType"></element>
    <element name="Output" type="string"></element>

    <complexType name="InputType">
    	<sequence>
    		<element name="Name1" type="string"></element>
    		<element name="Name2" type="string"></element>
    	</sequence>
    </complexType>
</schema>

Add new XQuery Transformation
In the list of expression functions we will see our custom function (customConcat)

(:: 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) {
        <ns0:Output>{ osbK3:customConcat($input1/ns0:Name1,$input1/ns0:Name2) }</ns0:Output>
};

declare variable $input1 as element(ns0:Input) external;

xf:InputToOutput($input1)

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

Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

29 Responses to “Oracle Service Bus 11g, Using Custom Xpath functions”

[…] Adres URL: Oracle Service Bus 11g, Using Custom Xpath functions « Oracle … […]

commenter

Fantastic article.

One question, why prefix is osbk3? Is possible to change this?

Thanks!

commenter

You could add your own prefix

declare namespace test1 = “http://nl.iteye/osb/custom/functions/OsbUtils”;

and then use : test1:customConcat()

Eric Elzinga | May 14th, 2010 at 8:35 am
commenter

Did it work for you ?

commenter

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!

commenter

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 […]

Alessandro Ilardo | November 23rd, 2010 at 3:44 pm
commenter

Does it work for 10.3 as well?

commenter

No. It’s new in the 11g release.

commenter

Thank you for nice article.

commenter

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. ;-)

commenter

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.

commenter

what is the datatype of the variable you’re trying to bind ? can you paste the code ?

commenter

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?

commenter

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

commenter

Warning: pack() [function.pack]: Type H: illegal hex digit L in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit R in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit I in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit S in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit G in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit J in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit v in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit p in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit t in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit T in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit R in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit g in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit L in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit h in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit j in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit M in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit I in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit M in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit I in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit o in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit m in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit y in /home/deb32501/domains/xenta.nl/public_html/blog/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

My e-mail address is hero...@hotmail.com
Thx

commenter

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)

commenter

I solved the problem by changing the jre version of the project to version 1.5

commenter

Even after i changed to JRE version 1.5, this error is not resolved

unknown function (or number of arguments (2) is
wrong)

commenter

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 […]

commenter

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.

commenter

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

commenter

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?

commenter

Hi Eric. Thanks for tour reply.

Yes, I already did that and it works!

Thank you.

commenter

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

commenter

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.

commenter

Hi,

Can someone please help me in doing this in weblogic integration 10.2 server.

Thanks in advance.

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>