Monday 5 September 2011

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>

Sunday 4 September 2011

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.