Tuesday, May 14, 2013

Developing and Deploying Custom XPath/XSL Functions

Apart from XPATH functions provided by Oracle,there are sometimes requirement that normal function cannot fulfil.To support this oracle has provided the privilege to create Custom XPATH functions,which can be used along with normal XPATH/XSL functions in your XSLs or Assign actions.

Consider one usecase where you have to remove all the special characters and spaces from a given input and return the trimmed output.For implementing this we will create one custom XSL function and use it in our project.

Step1: Create a java class and write down your function logic in that class.Compile and test it.

Sample Java Code:

package com;

public class RemoveSpecialCharacters {
    public static String removeChars(String input, String regex, String replaceemntString)
      String result = input.replaceAll(regex, replaceemntString);
      return result;

    public static void main(String[] args) {
        RemoveSpecialCharacters removeSpecialCharacters = new RemoveSpecialCharacters();
        String input = "((80)-IN855A.0+11)";
            System.out.println(removeChars(input, "[^A-Za-z^0-9]", ""));


Step2: Custom functions should be registered in JDeveloper to be able to show them with BPEL expression builder and in the XSL Mapper. To do that, provide a User Defined Extension Functions config file [ext-soa-xpath-functions-config.xml], as shown belwo, and register it with JDeveloper through FilePreferencesXSL Map.Modify this xml file to incoorporate your changes.
Sample XML:

<?xml version="1.0" encoding="UTF-8"?>
<soa-xpath-functions version="11.1.1"
    <function name="trimSC:removeChars">
    <return type="string"/>
       <param name = "input" type="string"/>
       <param name = "regex" type="string"/>
       <param name = "replaceemntString" type="string"/>
       <![CDATA[This function returns the value after removing all the special
       characters from the input except alphabets and integerse.]]>

Step3: Now we will create one jar file    for out function.Create a new folder and paste your class file with complete folder structure in it,In the same folder create one new folder named as "META-INF" and place your ext-soa-xpath-functions-config.xml in it.
Step4: Create jar file using "jar -cvf functionname.jar *.*" command.
Step5: Now custom function jar created in last step will be registered with JDev.Go to Tools --> Preferences.
Step6: Drill down to SOA tab and add your jar file using ADD Button.
Step7: Save and restart your JDev.Cross Check and verify your function under user defined functions.
Step8: We will test our new function now.I will be creating one dummy xsl using schema i used in DVM Tutorial published in my previous posts.Give the input,regular expression and replacement string.
Step9: Test your xsl file, you would be able to see the trimmed output in the target xml. 
In this way,you can create any custom XSLT functions depeding upon the requirement and use it in your project.
NOTE: To make this function available at SOA runtime place the jar file under $SOA_HOME/user_projects/domains/domain_name/lib.

Take restart of your server and test newly created function.

Happy Learning,


  1. "jar -cvf functionname.jar *.*" didn't work for me. I had to do "jar -cvf functionname.jar *" for it to add the directories. The article was a great help for me though.