Suppose you are getting 2 XML responses from 2 service endpoints like below.
summary.xml
<?xml version="1.0" encoding="UTF-8"?> <policyList xmlns="http://insurance.org/policyservice"> <policy> <policyType>Life</policyType> <holderName>Ann Frank</holderName> <policyId>SWXU9124</policyId> <premium>100000</premium> </policy> <policy> <policyType>Life</policyType> <holderName>Shane Watson</holderName> <policyId>SWXABS4</policyId> <premium>70000</premium> </policy> </policyList>
detail.xml
<?xml version="1.0" encoding="UTF-8"?> <policyList xmlns="http://insurance.org/policyservice"> <policy> <policyType>Life</policyType> <policyId>SWXABS4</policyId> <validityPeriod>3Years</validityPeriod> <startedDate>10/10/2014</startedDate> <claims> <claim> <id>2016AAASI900</id> <date>01/06/2016</date> <claimAmount>1400</claimAmount> </claim> </claims> </policy> <policy> <policyType>Life</policyType> <policyId>SWXU9124</policyId> <validityPeriod>1Years</validityPeriod> <startedDate>01/01/2016</startedDate> <claims> <claim> <id>2016TABSI981</id> <date>15/02/2016</date> <claimAmount>9900</claimAmount> </claim> </claims> </policy> </policyList>
Above two responses are dynamic responses. So, above 2 XMLs should be aggregated to one xml before applying XSLT mediator. For that you can use PayloadFactory Mediator, and get aggregated XML like below.
<?xml version="1.0" encoding="UTF-8"?> <policyList xmlns="http://insurance.org/policyservice"> <summary> <policy> <policyType>Life</policyType> <holderName>Ann Frank</holderName> <policyId>SWXU9124</policyId> <premium>100000</premium> </policy> <policy> <policyType>Life</policyType> <holderName>Shane Watson</holderName> <policyId>SWXABS4</policyId> <premium>70000</premium> </policy> </summary> <details> <policy> <policyType>Life</policyType> <policyId>SWXABS4</policyId> <validityPeriod>3Years</validityPeriod> <startedDate>10/10/2014</startedDate> <claims> <claim> <id>2016AAASI900</id> <date>01/06/2016</date> <claimAmount>1400</claimAmount> </claim> </claims> </policy> <policy> <policyType>Life</policyType> <policyId>SWXU9124</policyId> <validityPeriod>1Years</validityPeriod> <startedDate>01/01/2016</startedDate> <claims> <claim> <id>2016TABSI981</id> <date>15/02/2016</date> <claimAmount>9900</claimAmount> </claim> </claims> </policy> </details> </policyList>
Assume you want to get the response as follows.
<?xml version="1.0" encoding="UTF-8"?> <ns:policyList xmlns:ns="http://insurance.org/policyservice"> <policy xmlns="http://insurance.org/policyservice"> <policyType>Life</policyType> <holderName>Ann Frank</holderName> <policyId>SWXU9124</policyId> <premium>100000</premium> <claims> <claim> <id>2016TABSI981</id> <date>15/02/2016</date> <claimAmount>9900</claimAmount> </claim> </claims> <startedDate>01/01/2016</startedDate> <validityPeriod>1Years</validityPeriod> </policy> <policy xmlns="http://insurance.org/policyservice"> <policyType>Life</policyType> <holderName>Shane Watson</holderName> <policyId>SWXABS4</policyId> <premium>70000</premium> <claims> <claim> <id>2016AAASI900</id> <date>01/06/2016</date> <claimAmount>1400</claimAmount> </claim> </claims> <startedDate>10/10/2014</startedDate> <validityPeriod>3Years</validityPeriod> </policy> </ns:policyList>
You can use the XSLT mediator to above transformation. Use below XSL file with XSLT mediator. Upload following XSL file into registry.
merger.xslt
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sn="http://insurance.org/policyservice" version="1.0"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*" /> <xsl:key name="policy-by-id" match="sn:details/sn:policy" use="sn:policyId" /> <!-- identity transform --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <xsl:template match="/sn:policyList"> <xsl:copy> <xsl:apply-templates select="sn:summary/sn:policy" /> </xsl:copy> </xsl:template> <xsl:template match="sn:policy"> <xsl:copy> <xsl:apply-templates /> <xsl:copy-of select="key('policy-by-id', sn:policyId)/sn:claims" /> <xsl:copy-of select="key('policy-by-id', sn:policyId)/sn:startedDate" /> <xsl:copy-of select="key('policy-by-id', sn:policyId)/sn:validityPeriod" /> </xsl:copy> </xsl:template> </xsl:stylesheet>
Define XSLT mediator like this in your sequence
<xslt key="gov:/xslt/merger.xslt"/>
No comments:
Post a Comment