Showing posts with label XSL. Show all posts
Showing posts with label XSL. Show all posts

Friday, July 19, 2013

Adding nodes and creating variables in XSL - Oracle SOA 11G

Hi guys,

Today I am gonna write about,how to use summation function in XSL and how to create a cariable and use it in your xsl. Actually,there was a requirement in our project,which was like adding some values of some fields in the input and based upon the sum we required to pass some value to the target system.So, to accomplish this we used sum function, that is available in xslt.

I will be demonstrating it taking one example.Suppose we have some fields that are coming in the input such as A,B,C and we need to add all these values and see whether its sum is greater than zero or less than zero.If the sum is greater than 0, we will pass say "pass" and if its other way around we will pass say "fail" to the target system.I have create a sample BPEL process for this usecase.Process is very simple: it is exposed as webservice and taking input from the client and after doing aforesaid transformation and logic will publish the message in jms Queue.I will just explain the XSL part.


Step1: In my transformation, first I will create a variable that I will be using to storing the sum of all the required nodes. So in your xsl on target node,right click and add variable "tempVar".
Step2: Drag and drop "Sum" function from Mathematical functions in the centre of xsl.
Step3: Now in the function add all the nodes that you want to add separated by "|".In this example I am adding surcharge,tax,vat,shipmentcharge nodes.






 <xsl:variable name="tempVar"
                select="sum((/imp1:employee/imp1:surcharge | (/imp1:employee/imp1:order/imp1:Tax | (/imp1:employee/imp1:order/imp1:VAT | /imp1:employee/imp1:order/imp1:shipmentCharge))))"/>


Step4: Based upon the sum we need to send value in the target XML.So in our case if its greater tha 0, We will send "Invocie" in typecode tag and "Credit MEmo" otherwise.To implement this add a choose when and otherwise condition. 
Step5: It will check the value of "tempVar" and based on the result,value will be set in the target xml.

Step6: Now deploy and test your composite. First we will test for positive vsalue.I have passed values in all the field greater than 0.As per our logic value in Typecode tag is sent as "INVOICE".
Step7: To check the opposite scenario,pass values such that sum is less than 0.Values now passed is "Credit Memo".
In this way we can use the sum function and also you learned ,how to create a variable in xsl and use that in your XSL.







NOTE: There can be scenarios where values in some nodes can be empty.So you have to handle that as well,otherwise,you will get "NaN" in your result.Hence in order to handle blank nodes use below expresssion:

sum((/imp1:employee/imp1:surcharge[. != ""] | (/imp1:employee/imp1:order/imp1:Tax[. != ""] | (/imp1:employee/imp1:order/imp1:VAT[. != ""] | /imp1:employee/imp1:order/imp1:shipmentCharge[. != ""]))))
  
Hope this post is useful to you guys.

Happy Learning,
Cheers !!!

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"
   xmlns="http://xmlns.oracle.com/soa/config/xpath"
   xmlns:trimSC="http://www.oracle.com/XSL/Transform/java/com.RemoveSpecialCharacters"
   >
    <function name="trimSC:removeChars">
    <className>com.RemoveSpecialCharacters</className>
    <return type="string"/>
    <params>
       <param name = "input" type="string"/>
       <param name = "regex" type="string"/>
       <param name = "replaceemntString" type="string"/>
   </params>
    <desc/>
     <detail>
       <![CDATA[This function returns the value after removing all the special
       characters from the input except alphabets and integerse.]]>
    </detail>
   </function>
 
</soa-xpath-functions>

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,
Cheers