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,
Then using synchronous read I’m reading second file whose content is based on below schema,
Now after merging I want the content structured as below where CustomerId is common key between two files,
and we have to do this merge in xslt , here is the simple code to achieve this,
<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>
Then using synchronous read I’m reading second file whose content is based on below schema,
Now after merging I want the content structured as below where CustomerId is common key between two files,
and we have to do this merge in xslt , here is the simple code to achieve this,
<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>
No comments:
Post a Comment