Showing posts with label XSLT. Show all posts
Showing posts with label XSLT. Show all posts

Thursday, 16 February 2017

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 !!!

Wednesday, 14 September 2016

String to Date Conversion in XSLT

This blog might be handy for you for quick date conversion from string to date. Like in our source is String DD-MON-YYYY format and I want to convert it to YYYY-MM-DD for making the JCA adapter recognize the same during updation in oracle table.I didn’t find out any in built function is available so I decided to use simple sql query in xslt.
image
I just used query-database function and sql query is constructed using string concatenation.
image
and if you look at the database function ,
image
and whole sqlquery is concat("select to_char(to_date('",/ns0:arrivalDate,"','DD-MON-YYYY'),'YYYY-MM-DD') arrivalDate from dual")
handy for me as well for future reference.

Thursday, 31 March 2016

Merge Two Source Element based on a Common Key in XSLT

I had a requirement where I need to merge the contents of two files into a single file and there was a common key between the two schema.We start our process by polling into first file location based on follow schema,
image
Then using synchronous read I’m reading second file whose content is based on below schema,
image
Now after merging I want the content structured as below where CustomerId is common key between two files,
image
and we have to do this merge in xslt , here is the simple code to achieve this,
image
   <xsl:template match="/">
    <ns0:CustomerInfoFinal>
      <xsl:for-each select="/ns1:CustomerInfo1/ns1:Customer">
        <xsl:variable name="custID" select="ns1:CustomerId"/>
        <ns0:Customer>
          <ns0:CustomerId>
            <xsl:value-of select="ns1:CustomerId"/>
          </ns0:CustomerId>
          <ns0:Name>
            <xsl:value-of select="ns1:Name"/>
          </ns0:Name>
          <ns0:PAN>
            <xsl:value-of select="ns1:PAN"/>
          </ns0:PAN>
          <ns0:Address>
            <xsl:value-of select="$custDetails/ns2:CustomerInfo2/ns2:CustomerOtherInfo[(ns2:CustomerId = $custID)]/ns2:Address"/>
          </ns0:Address>
          <ns0:Age>
            <xsl:value-of select="$custDetails/ns2:CustomerInfo2/ns2:CustomerOtherInfo[(ns2:CustomerId = $custID)]/ns2:Age"/>
          </ns0:Age>
          <ns0:Gender>
            <xsl:value-of select="$custDetails/ns2:CustomerInfo2/ns2:CustomerOtherInfo[(ns2:CustomerId = $custID)]/ns2:Gender"/>
          </ns0:Gender>
        </ns0:Customer>
      </xsl:for-each>
    </ns0:CustomerInfoFinal>
  </xsl:template>

Wednesday, 9 March 2016

Search & Replace JDBC Data source in XSLT using ANT

Search & Replace JDBC Data source in XSLT using ANT

Hey everybody, today I come across a problem while deploying my code using ANT script.Each of my composite has its own deployment plan by which all the endpoint reference and jca properties getting changed to new environment.. but in the XSLT I’ve used couple of query database XPATH function and it use datasource as one of its input parameter.But I didn’t find any option in deployment plan to change the datasource as per my new environment.What if, we have a helper ant script that will search all the xsl files in a directory recursively and work in conjunction with main ant build script, definitely would be good. To get started I created a simple java class who will do the search and replacement job of all xslt files,
package com.shrik.utility;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SearchAndReplaceXSLT {
    public static void main(String[] args) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("trx.properties"));
     
        File root = new File(properties.getProperty("FileRoot"));
     
        System.out.println(properties.getProperty("FileRoot"));
        String[] extensions = { properties.getProperty("FileExt") };
        boolean recursive = true;
        Collection files = FileUtils.listFiles(root, extensions, recursive);
        for (Iterator iterator = files.iterator(); iterator.hasNext(); ) {
            File file = (File)iterator.next();
            System.out.println("File = " + file.getAbsolutePath());
            SearchAndReplace(file,properties.getProperty("SearchStr"),properties.getProperty("ReplaceStr"));
        }
    }
   private static void SearchAndReplace(File file, String searchStr,
                                         String replaceStr) throws IOException {
        StringBuffer sb=new StringBuffer();
        Pattern pattern=Pattern.compile(searchStr);
        Matcher matcher=pattern.matcher(FileUtils.readFileToString(file));
        while(matcher.find()){
            matcher.appendReplacement(sb, replaceStr);
         
        }
        matcher.appendTail(sb);
        System.out.println(sb.toString());
        FileUtils.writeStringToFile(file,sb.toString() );
    }
}
and here is the corresponding trx.properties file,
FileRoot=C:/shrik/Oracle/SOAWork/composites/DeploymentFramework/test/trx
FileExt=xsl
SearchStr=DS_DEV_YYY&
ReplaceStr=DS_DEV_ZZZ&
build.dir=build
lib.dir=lib
main-class="com.shrik.utility.SearchAndReplaceXSLT"
Do fill up all the values based on your environment and root code repository directory.Just give a test run and check whether its doing your job or not.
Then create a jar file pointing your main class in manifest file.Here we have a dependency on org.apache.commons.io.FileUtils class so I created a folder lib and put the commons-io-1.3.2.jar file under that, here is my directory structure,
image
Under the build folder I created another directory jar , and under that I placed SearchAndReplaceXSLT.jar file.I created a trx folder where I copied all the xsl files, datasource need to be replaced.Now we need to refer the jar file from a ant script and for that I created build.xml file which contains,
<project name="SearchAndReplaceXSLT" basedir="." default="run">
<property file="trx.properties"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
     <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
    </path>
 
    <target name="run" >
        <java fork="true" classname="${main-class}">
            <classpath>
                <path refid="classpath"/>
                <path location="${jar.dir}/${ant.project.name}.jar"/>
            </classpath>
        </java>
    </target>
</project>
Now just run ant and it will do the job .
So next part is how will I call this ant file from main build.xml in deployment script?
In main build.properties file I created a new property trx.amendment=true . Based on this value it will call the child script as below,
<target name="deployAll">
        <if>
          <equals arg1="${trx.amendment}" arg2="true"/>
          <then>
             <ant antfile="${env.CURRENT_FOLDER}/trx.xml" inheritAll="false" target="run"/>
          </then>
      </if>      
     
         <if>
          <equals arg1="${mds.enabled}" arg2="true"/>
          <then>
             <antcall target="deployMDS" inheritall="true"/>
          </then>
      </if>   
      <foreach list="${applications}" param="application" target="deployApplication" inheritall="true" inheritrefs="false"/>
    </target>
So with this you can easily change any data source name in sequence or database query function of your XSLT.

String to Date Conversion in XSLT

String to Date Conversion in XSLT

This blog might be handy for you for quick date conversion from string to date. Like in our source is String DD-MON-YYYY format and I want to convert it to YYYY-MM-DD for making the JCA adapter recognize the same during updation in oracle table.I didn’t find out any in built function is available so I decided to use simple sql query in xslt.
image
I just used query-database function and sql query is constructed using string concatenation.
image
and if you look at the database function ,
image
and whole sqlquery is concat("select to_char(to_date('",/ns0:arrivalDate,"','DD-MON-YYYY'),'YYYY-MM-DD') arrivalDate from dual")
handy for me as well for future reference.

Merge Two Source Element based on a Common Key in XSLT

Merge Two Source Element based on a Common Key in XSLT

I had a requirement where I need to merge the contents of two files into a single file and there was a common key between the two schema.We start our process by polling into first file location based on follow schema,
image
Then using synchronous read I’m reading second file whose content is based on below schema,
image
Now after merging I want the content structured as below where CustomerId is common key between two files,
image
and we have to do this merge in xslt , here is the simple code to achieve this,
image
   <xsl:template match="/">
    <ns0:CustomerInfoFinal>
      <xsl:for-each select="/ns1:CustomerInfo1/ns1:Customer">
        <xsl:variable name="custID" select="ns1:CustomerId"/>
        <ns0:Customer>
          <ns0:CustomerId>
            <xsl:value-of select="ns1:CustomerId"/>
          </ns0:CustomerId>
          <ns0:Name>
            <xsl:value-of select="ns1:Name"/>
          </ns0:Name>
          <ns0:PAN>
            <xsl:value-of select="ns1:PAN"/>
          </ns0:PAN>
          <ns0:Address>
            <xsl:value-of select="$custDetails/ns2:CustomerInfo2/ns2:CustomerOtherInfo[(ns2:CustomerId = $custID)]/ns2:Address"/>
          </ns0:Address>
          <ns0:Age>
            <xsl:value-of select="$custDetails/ns2:CustomerInfo2/ns2:CustomerOtherInfo[(ns2:CustomerId = $custID)]/ns2:Age"/>
          </ns0:Age>
          <ns0:Gender>
            <xsl:value-of select="$custDetails/ns2:CustomerInfo2/ns2:CustomerOtherInfo[(ns2:CustomerId = $custID)]/ns2:Gender"/>
          </ns0:Gender>
        </ns0:Customer>
      </xsl:for-each>
    </ns0:CustomerInfoFinal>
  </xsl:template>

Thursday, 19 November 2015

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 !!!