Question

I have a requirement to add an extra section under TXLifeRequest for summed up payment amount from the below xml.

<?xml version="1.0" encoding="utf-8"?>
<TXLife xmlns="http://ACORD.org/Standards/Life/2">
    <TXLifeRequest>
        <FundCode>LTRT00</FundCode>
        <AccountDescription>CWA xxx</AccountDescription>
        <CurrencyTypeCode>840</CurrencyTypeCode>
        <TransExeDate>2013-04-20</TransExeDate>
        <AccountNumber>34142</AccountNumber>
        <PaymentAmt>300.000000000</PaymentAmt>
        <ReversalInd>0</ReversalInd>
    </TXLifeRequest>
    <TXLifeRequest>
        <FundCode>LTRW00</FundCode>
        <AccountDescription>CWA xxx</AccountDescription>
        <CurrencyTypeCode>840</CurrencyTypeCode>
        <TransExeDate>2013-04-20</TransExeDate>
        <AccountNumber>34142</AccountNumber>
        <PaymentAmt>300.000000000</PaymentAmt>
        <ReversalInd>0</ReversalInd>
    </TXLifeRequest>
    <TXLifeRequest>
        <FundCode>LTRW00</FundCode>
        <AccountDescription>CWA xxx</AccountDescription>
        <CurrencyTypeCode>840</CurrencyTypeCode>
        <TransExeDate>2013-04-20</TransExeDate>
        <AccountNumber>34142</AccountNumber>
        <PaymentAmt>300.000000000</PaymentAmt>
        <ReversalInd>0</ReversalInd>
    </TXLifeRequest>
    <TXLifeRequest>
        <FundCode>LUL500</FundCode>
        <AccountDescription>CWA xxx</AccountDescription>
        <CurrencyTypeCode>840</CurrencyTypeCode>
        <TransExeDate>2013-04-20</TransExeDate>
        <AccountNumber>34142</AccountNumber>
        <PaymentAmt>800.000000000</PaymentAmt>
        <ReversalInd>1</ReversalInd>
    </TXLifeRequest>
</TXLife>

The criteria to sum up PaymentAmt is to look for combination TransExeDate , AccountNumber and ReversalInd. So based on these criteria when i apply my below xslt PaymentAmt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" xmlns:ns="http://ACORD.org/Standards/Life/2">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="detKey7s" match="//ns:TXLifeRequest"
        use="concat(generate-id(..), ns:TransExeDate, '+', ns:FundCode, '+', ns:ReversalInd)"/>

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

    <xsl:template match="/ns:TXLife">
        <xsl:element name="TXLife" namespace="http://ACORD.org/Standards/Life/2">
        <xsl:for-each select="ns:TXLifeRequest[generate-id() = generate-id(key('detKey7s',
            concat(generate-id(..), ns:TransExeDate, '+', ns:FundCode, '+', ns:ReversalInd)
            )
            )
            ]
            ">
            <xsl:copy>

                <xsl:variable name="vDataGroup" select=
                    "key('detKey7s', concat(generate-id(..),ns:TransExeDate, '+', ns:FundCode, '+', ns:ReversalInd))"/>                
                <xsl:apply-templates select="./*"></xsl:apply-templates>               
                <xsl:element name="TotalAmount" namespace="http://ACORD.org/Standards/Life/2">
                    <xsl:value-of select="sum($vDataGroup/ns:PaymentAmt)"/>
                </xsl:element>

            </xsl:copy>  

        </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

So after applying above xslt i will get summed up TXLifeRequest xml see <TotalAmount>600</TotalAmount>.

<TXLife xmlns="http://ACORD.org/Standards/Life/2">
   <TXLifeRequest>
      <FundCode>LTRT00</FundCode>
      <AccountDescription>CWA – NB+U</AccountDescription>
      <CurrencyTypeCode>840</CurrencyTypeCode>
      <TransExeDate>2013-04-20</TransExeDate>
      <AccountNumber>34142</AccountNumber>
      <PaymentAmt>300.000000000</PaymentAmt>
      <ReversalInd>0</ReversalInd>
      <TotalAmount>300</TotalAmount>
   </TXLifeRequest>
   <TXLifeRequest>
      <FundCode>LTRW00</FundCode>
      <AccountDescription>CWA – NB+U</AccountDescription>
      <CurrencyTypeCode>840</CurrencyTypeCode>
      <TransExeDate>2013-04-20</TransExeDate>
      <AccountNumber>34142</AccountNumber>
      <PaymentAmt>300.000000000</PaymentAmt>
      <ReversalInd>0</ReversalInd>
      <TotalAmount>600</TotalAmount>
   </TXLifeRequest>
   <TXLifeRequest>
      <FundCode>LUL500</FundCode>
      <AccountDescription>CWA – NB+U</AccountDescription>
      <CurrencyTypeCode>840</CurrencyTypeCode>
      <TransExeDate>2013-04-20</TransExeDate>
      <AccountNumber>34142</AccountNumber>
      <PaymentAmt>800.000000000</PaymentAmt>
      <ReversalInd>1</ReversalInd>
      <TotalAmount>800</TotalAmount>
   </TXLifeRequest>
</TXLife>

Now the question is how do i add an extra section right under summed up section? So finally the xml should look as mentioned below

<TXLifeRequest>
      <FundCode>LTRT00</FundCode>
      <AccountDescription>CWA – NB+U</AccountDescription>
      <CurrencyTypeCode>840</CurrencyTypeCode>
      <TransExeDate>2013-04-20</TransExeDate>
      <AccountNumber>34142</AccountNumber>
      <PaymentAmt>300.000000000</PaymentAmt>
      <ReversalInd>0</ReversalInd>
      <TotalAmount>300</TotalAmount>
   </TXLifeRequest>
   <TXLifeRequest>
      <FundCode>LTRW00</FundCode>
      <AccountDescription>CWA – NB+U</AccountDescription>
      <CurrencyTypeCode>840</CurrencyTypeCode>
      <TransExeDate>2013-04-20</TransExeDate>
      <AccountNumber>34142</AccountNumber>
      <PaymentAmt>300.000000000</PaymentAmt>
      <ReversalInd>0</ReversalInd>
      <TotalAmount>600</TotalAmount>
   </TXLifeRequest>

   <TXLifeRequest>
      <SummedUP>LTRW00</SummedUP>
   </TXLifeRequest>


   <TXLifeRequest>
      <FundCode>LUL500</FundCode>
      <AccountDescription>CWA – NB+U</AccountDescription>
      <CurrencyTypeCode>840</CurrencyTypeCode>
      <TransExeDate>2013-04-20</TransExeDate>
      <AccountNumber>34142</AccountNumber>
      <PaymentAmt>800.000000000</PaymentAmt>
      <ReversalInd>1</ReversalInd>
      <TotalAmount>800</TotalAmount>
   </TXLifeRequest>

I am struck with adding a section just after the summed up section as mentioned above. Please let me know how do i proceed. Any ideas ??

Thanks, Madhu CM

Was it helpful?

Solution

This is only a smal change to your current xsl:stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" xmlns:ns="http://ACORD.org/Standards/Life/2"
    xmlns="http://ACORD.org/Standards/Life/2">

    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="detKey7s" match="//ns:TXLifeRequest"
        use="concat(generate-id(..), ns:TransExeDate, '+', ns:FundCode, '+', ns:ReversalInd)"/>

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

    <xsl:template match="/ns:TXLife">
        <xsl:element name="TXLife" namespace="http://ACORD.org/Standards/Life/2">
            <xsl:for-each select="ns:TXLifeRequest[generate-id() = generate-id(key('detKey7s',
            concat(generate-id(..), ns:TransExeDate, '+', ns:FundCode, '+', ns:ReversalInd)
            )
            )
            ]
            ">
                <xsl:variable name="vDataGroup" select=
                    "key('detKey7s', concat(generate-id(..),ns:TransExeDate, '+', ns:FundCode, '+', ns:ReversalInd))"/>

                <xsl:copy>

                    <xsl:apply-templates select="./*"></xsl:apply-templates>
                    <xsl:element name="TotalAmount" namespace="http://ACORD.org/Standards/Life/2">
                        <xsl:value-of select="sum($vDataGroup/ns:PaymentAmt)"/>
                    </xsl:element>

                </xsl:copy>
                <xsl:if test="count($vDataGroup) >1" >
                    <TXLifeRequest>
                        <SummedUP>
                            <xsl:value-of select=" ns:FundCode"/>
                        </SummedUP>
                    </TXLifeRequest>
                </xsl:if>

            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Which will generate following output:

<TXLife xmlns="http://ACORD.org/Standards/Life/2">
  <TXLifeRequest>
    <FundCode>LTRT00</FundCode>
    <AccountDescription>CWA xxx</AccountDescription>
    <CurrencyTypeCode>840</CurrencyTypeCode>
    <TransExeDate>2013-04-20</TransExeDate>
    <AccountNumber>34142</AccountNumber>
    <PaymentAmt>300.000000000</PaymentAmt>
    <ReversalInd>0</ReversalInd>
    <TotalAmount>300</TotalAmount>
  </TXLifeRequest>
  <TXLifeRequest>
    <FundCode>LTRW00</FundCode>
    <AccountDescription>CWA xxx</AccountDescription>
    <CurrencyTypeCode>840</CurrencyTypeCode>
    <TransExeDate>2013-04-20</TransExeDate>
    <AccountNumber>34142</AccountNumber>
    <PaymentAmt>300.000000000</PaymentAmt>
    <ReversalInd>0</ReversalInd>
    <TotalAmount>600</TotalAmount>
  </TXLifeRequest>
  <TXLifeRequest>
    <SummedUP>LTRW00</SummedUP>
  </TXLifeRequest>
  <TXLifeRequest>
    <FundCode>LUL500</FundCode>
    <AccountDescription>CWA xxx</AccountDescription>
    <CurrencyTypeCode>840</CurrencyTypeCode>
    <TransExeDate>2013-04-20</TransExeDate>
    <AccountNumber>34142</AccountNumber>
    <PaymentAmt>800.000000000</PaymentAmt>
    <ReversalInd>1</ReversalInd>
    <TotalAmount>800</TotalAmount>
  </TXLifeRequest>
</TXLife>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top