1
votes

I need to pass the XML to third party system which can be understand by third party and parse it.

Below is my input xml which I created data by fetching from database.

<FIXML>
    <Header>
        <RequestID>ReqID8942</RequestID>
        <RequestType>DocGen</RequestType>
        <Version>10.6</Version>
        <BankId>01</BankId>
        <ChannelId>LOS</ChannelId>
    </Header>
    <Body>
        <Data>
            **<CorpAppLimitDetailsBO>
            <ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
            <ApprovedLimitCCY>INR</ApprovedLimitCCY>
            <ApprovedLimit>100.0</ApprovedLimit>
            <LimitClassification>ROOT</LimitClassification>
        </CorpAppLimitDetailsBO>
        <CorpAppLimitDetailsBO>
            <ApprovedLimitHomeCCY>0.0</ApprovedLimitHomeCCY>
            <ApprovedLimitCCY/>
            <ApprovedLimit>500.0</ApprovedLimit>
            <LimitClassification>CLASSIFICATION1</LimitClassification>
        </CorpAppLimitDetailsBO>
        <CorpAppLimitDetailsBO>
            <ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
            <ApprovedLimitCCY>INR</ApprovedLimitCCY>
            <ApprovedLimit>100.0</ApprovedLimit>
            <LimitClassification>CLASSIFICATION1</LimitClassification>
        </CorpAppLimitDetailsBO>
        <CorpAppProductDetailsBO>
            <ProductCategory>3</ProductCategory>
        </CorpAppProductDetailsBO>
        <CorpAppProductDetailsBO>
            <ProductCategory>1</ProductCategory>
        </CorpAppProductDetailsBO>
        <CorpAppProductDetailsBO>
            <ProductCategory>2</ProductCategory>
            </CorpAppProductDetailsBO>**
            <TemplateDetails>
                <Template>tempid001</Template>
            </TemplateDetails>
            <SelectedClauses>
                <Clauses>
                    <Clause>clause1</Clause>
                </Clauses>
                <Clauses>
                    <Clause>clause2</Clause>
                </Clauses>
                <Clauses>
                    <Clause>clause3</Clause>
                </Clauses>
            </SelectedClauses>
            <Distribution>
                <Email>[email protected],[email protected],[email protected]</Email>
                <Print>blrkec3030,blrkec3031</Print>
            </Distribution>
        </Data>
    </Body>
</FIXML>

I want to convert this input XML to another XML format using XSLT.

Below is the format which I need,

<FIXML>
    <Header>
        <RequestID>ReqID8942</RequestID>
        <RequestType>DocGen</RequestType>
        <Version>10.6</Version>
        <BankId>01</BankId>
        <ChannelId>LOS</ChannelId>
    </Header>
    <Body>
        <Data>
            **<LimitDetails>
            <Limit>
                <ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
                <ApprovedLimitCCY>INR</ApprovedLimitCCY>
                <ApprovedLimit>100.0</ApprovedLimit>
                <LimitClassification>ROOT</LimitClassification>
            </Limit>
            <Limit>
                <ApprovedLimitHomeCCY>0.0</ApprovedLimitHomeCCY>
                <ApprovedLimitCCY/>
                <ApprovedLimit>500.0</ApprovedLimit>
                <LimitClassification>CLASSIFICATION1</LimitClassification>
            </Limit>
            <Limit>
                <ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
                <ApprovedLimitCCY>INR</ApprovedLimitCCY>
                <ApprovedLimit>100.0</ApprovedLimit>
                <LimitClassification>CLASSIFICATION1</LimitClassification>
            </Limit>
        </LimitDetails>
        <ProductDetails>
            <Product>
                <ProductCategory>3</ProductCategory>
            </Product>
            <Product>
                <ProductCategory>1</ProductCategory>
            </Product>
            <Product>
                <ProductCategory>2</ProductCategory>
            </Product>
            </ProductDetails>**
            <TemplateDetails>
                <Template>tempid001</Template>
            </TemplateDetails>
            <SelectedClauses>
                <Clauses>
                    <Clause>clause1</Clause>
                </Clauses>
                <Clauses>
                    <Clause>clause2</Clause>
                </Clauses>
                <Clauses>
                    <Clause>clause3</Clause>
                </Clauses>
            </SelectedClauses>
            <Distribution>
                <Email>[email protected],[email protected],[email protected]</Email>
                <Print>blrkec3030,blrkec3031</Print>
            </Distribution>
        </Data>
    </Body>
</FIXML>

Please help me out as I have to complete the task in another 2 days of time.

I tried with below code and i m getting below outputs but rest of the tags for e.g. <FIXML> , <TemplateDetails> and etc is not coming as part of output xml.

xsl code below:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output indent="yes" />    <!-- This identity template copies the document -->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
    </xsl:template>

    <!--
        This template will only match the 'CorpAppLimitDetailsBO' 
        nodes and modify them the way you want.
    -->

<xsl:template match="/*">
        <xsl:element name="LimitDetails">
            <xsl:for-each select="//CorpAppLimitDetailsBO">
                <xsl:element name="Limit">
                    <xsl:for-each select="*">
                        <xsl:copy-of select="." />
                    </xsl:for-each>
                </xsl:element>
            </xsl:for-each>
        </xsl:element>
        <xsl:element name="ProductDetails">
            <xsl:for-each select="//CorpAppProductDetailsBO">
                <xsl:element name="Product">
                    <xsl:for-each select="*">
                        <xsl:copy-of select="." />
                    </xsl:for-each>
                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>



</xsl:stylesheet>

output.xml below:

<?xml version="1.0" encoding="UTF-8"?>
<LimitDetails>
<Limit>
<ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
<ApprovedLimitCCY>INR</ApprovedLimitCCY>
<ApprovedLimit>100.0</ApprovedLimit>
<LimitClassification>ROOT</LimitClassification>
</Limit>
<Limit>
<ApprovedLimitHomeCCY>0.0</ApprovedLimitHomeCCY>
<ApprovedLimitCCY/>
<ApprovedLimit>500.0</ApprovedLimit>
<LimitClassification>CLASSIFICATION1</LimitClassification>
</Limit>
<Limit>
<ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
<ApprovedLimitCCY>INR</ApprovedLimitCCY>
<ApprovedLimit>100.0</ApprovedLimit>
<LimitClassification>CLASSIFICATION1</LimitClassification>
</Limit>
</LimitDetails>
<ProductDetails>
<Product>
<ProductCategory>3</ProductCategory>
</Product>
<Product>
<ProductCategory>1</ProductCategory>
</Product>
<Product>
<ProductCategory>2</ProductCategory>
</Product>
</ProductDetails>

Note: The child tag(for e.g.ApprovedLimitHomeCCY....) which i present under BO's(for e.g. CorpAppLimitDetailsBO) tag are dynamic.I shouldnt hard code in xsl.I m new to XSLT. pls help me out.

Thanks Shil and Sean for your solution. both are perfect for my requirements. but i have one more doubt now.i m adding one more child tag <DBApplicantMiscDetails> under

<CorpAppProductDetailsBO>

Input xml:

   <CorpAppProductDetailsBO>
    <ProductCategory>2</ProductCategory>
        <DBApplicantMiscDetails>
            <APPLICANTMISCID>400000</APPLICANTMISCID>
            <APPLICANTID>400030</APPLICANTID>
            <MISCTYPE>APPLIED</MISCTYPE>
        </DBApplicantMiscDetails>
   </CorpAppProductDetailsBO>

Below is the output format which i expect.

<ProductDetails>
<Product>
 <ProductCategory>2</ProductCategory>
 <APPLICANTMISCID>400000</APPLICANTMISCID>
 <APPLICANTID>400030</APPLICANTID>
 <MISCTYPE>APPLIED</MISCTYPE>
</Product>
</ProductDetails>

Thanks again.

3
You might want to try first and post what you've tried and the results if you expect helpful responsesGratzy
Thanks for the response.I posted code which i triedMoorthy
Hey @Moorthy are you going to accept an answer?Sean B. Durkin
Hey @Sean.I have accepted your answer.I posted one more query i added one more child tag<DBApplicantMiscDetails> under <CorpAppProductDetailsBO>. could you please help me on this?Moorthy

3 Answers

0
votes
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:key name="kDetails" match="*
   [starts-with(name(),'CorpApp') and
    substring(name(), string-length(name()) - 8) = 'DetailsBO']"
         use="substring-before(name(),'DetailsBO')" />

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*[*[key('kDetails',substring-before(name(),'DetailsBO'))]]">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="*[generate-id() =
    generate-id(key('kDetails',
     substring-before(name(),'DetailsBO'))[1])]" mode="group" />
    <xsl:apply-templates select="*[not(
       key('kDetails',substring-before(name(),'DetailsBO')))]
       |comment()|processing-instruction()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="group">
  <xsl:variable name="group-name" select="substring-after(substring-before(name(),'DetailsBO'),'CorpApp')" />
  <xsl:element name="{$group-name}Details">
   <xsl:for-each select="key('kDetails',substring-before(name(),'DetailsBO'))">
     <xsl:element name="{$group-name}">
       <xsl:apply-templates select="@*|node()"/>
     </xsl:element>
   </xsl:for-each>  
  </xsl:element>
</xsl:template>

</xsl:stylesheet>
0
votes
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:apply-templates select="FIXML"/>
    </xsl:template>
    <xsl:template match="FIXML">
        <FIXML>
            <xsl:apply-templates select="Header"/>
            <xsl:apply-templates select="Body"/>
        </FIXML>
    </xsl:template>
    <xsl:template match="Header">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Body">
        <Body>
            <xsl:apply-templates select="Data"/>
        </Body>
    </xsl:template>
    <xsl:template match="Data">
        <Data>
            <LimitDetails>
                <xsl:apply-templates select="CorpAppLimitDetailsBO"/>
            </LimitDetails>
            <ProductDetails>
                <xsl:apply-templates select="CorpAppProductDetailsBO"/>
            </ProductDetails>
            <xsl:apply-templates select="TemplateDetails"/>
            <xsl:apply-templates select="SelectedClauses"/>
            <xsl:apply-templates select="Distribution"/>
        </Data>
    </xsl:template>
    <xsl:template match="CorpAppLimitDetailsBO">
        <Limit>
            <xsl:copy-of select="child::*"/>
        </Limit>
    </xsl:template>
    <xsl:template match="CorpAppProductDetailsBO">
        <xsl:apply-templates select="ProductCategory"/>
    </xsl:template>
    <xsl:template match="ProductCategory">
        <Product>
            <xsl:copy-of select="."/>
        </Product>
    </xsl:template>
    <xsl:template match="TemplateDetails">
    <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="SelectedClauses">
    <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Distribution">
    <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>
0
votes
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:apply-templates select="FIXML"/>
    </xsl:template>
    <xsl:template match="FIXML">
        <FIXML>
            <xsl:apply-templates select="Header"/>
            <xsl:apply-templates select="Body"/>
        </FIXML>
    </xsl:template>
    <xsl:template match="Header">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Body">
        <Body>
            <xsl:apply-templates select="Data"/>
        </Body>
    </xsl:template>
    <xsl:template match="Data">
        <Data>
            <LimitDetails>
                <xsl:apply-templates select="CorpAppLimitDetailsBO"/>
            </LimitDetails>
            <ProductDetails>
                <xsl:apply-templates select="CorpAppProductDetailsBO"/>
            </ProductDetails>
            <xsl:apply-templates select="TemplateDetails"/>
            <xsl:apply-templates select="SelectedClauses"/>
            <xsl:apply-templates select="Distribution"/>
        </Data>
    </xsl:template>
    <xsl:template match="CorpAppLimitDetailsBO">
        <Limit>
            <xsl:copy-of select="child::*"/>
        </Limit>
    </xsl:template>
    <xsl:template match="CorpAppProductDetailsBO">
    <Product>
        <xsl:apply-templates select="ProductCategory"/>
         <xsl:apply-templates select="DBApplicantMiscDetails"/>
    </Product>
    </xsl:template>
    <xsl:template match="ProductCategory">
            <xsl:copy-of select="."/>
    </xsl:template>
     <xsl:template match="DBApplicantMiscDetails">
    <xsl:copy-of select="child::*"/>
    </xsl:template>
    <xsl:template match="TemplateDetails">
    <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="SelectedClauses">
    <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Distribution">
    <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>