Hey all,

I have requirement, on the basis of indicator = 'TL' I have to pick CoverageName and Limit and add the amounts of similiar coverages. Is there any way I could do that.

Here's xml:

<Policy>
<Vehicle id="v1">
<Coverage>
<CoverageName>ABC</CoverageName>
<CoverageLimit>Upto 50,000</CoverageLimit>
<CoverageAmount>100</CoverageAmount>
<Indicator>TL</Indicator>
</Coverage>
<Coverage>
<CoverageName>EFG</CoverageName>
<CoverageLimit>Upto 50,000</CoverageLimit>
<CoverageAmount>50</CoverageAmount>
<Indicator></Indicator>
</Coverage>
<Coverage>
<CoverageName>HIJ</CoverageName>
<CoverageLimit>Upto 10,000</CoverageLimit>
<CoverageAmount>20</CoverageAmount>
<Indicator></Indicator>
</Coverage>
<Coverage>
<CoverageName>KLM</CoverageName>
<CoverageLimit>Upto 100,000</CoverageLimit>
<CoverageAmount>10</CoverageAmount>
<Indicator>TL</Indicator>
</Coverage>
</Vehicle>
<Vehicle id="v2">
<Coverage>
<CoverageName>ABC</CoverageName>
<CoverageLimit>Upto 50,000</CoverageLimit>
<CoverageAmount>100</CoverageAmount>
<Indicator>TL</Indicator>
</Coverage>
<Coverage>
<CoverageName>EFG</CoverageName>
<CoverageLimit>Upto 50,000</CoverageLimit>
<CoverageAmount>50</CoverageAmount>
<Indicator></Indicator>
</Coverage>
<Coverage>
<CoverageName>HIJ</CoverageName>
<CoverageLimit>Upto 10,000</CoverageLimit>
<CoverageAmount>20</CoverageAmount>
<Indicator></Indicator>
</Coverage>
<Coverage>
<CoverageName>KLM</CoverageName>
<CoverageLimit>Upto 100,000</CoverageLimit>
<CoverageAmount>10</CoverageAmount>
<Indicator>TL</Indicator>
</Coverage></Vehicle>
<Vehicle id="v3">
<Coverage>
<CoverageName>ABC</CoverageName>
<CoverageLimit>Upto 50,000</CoverageLimit>
<CoverageAmount>100</CoverageAmount>
<Indicator>TL</Indicator>
</Coverage>
<Coverage>
<CoverageName>EFG</CoverageName>
<CoverageLimit>Upto 50,000</CoverageLimit>
<CoverageAmount>50</CoverageAmount>
<Indicator></Indicator>
</Coverage>
<Coverage>
<CoverageName>HIJ</CoverageName>
<CoverageLimit>Upto 10,000</CoverageLimit>
<CoverageAmount>20</CoverageAmount>
<Indicator></Indicator>
</Coverage>
<Coverage>
<CoverageName>KLM</CoverageName>
<CoverageLimit>Upto 100,000</CoverageLimit>
<CoverageAmount>10</CoverageAmount>
<Indicator>TL</Indicator>
</Coverage>
</Vehicle>
</Policy>


The output I want is in three columns

ABC Upto 50,000 300
KLM Upto 100,000 30


Thankx in advance

Here is one way of doing what you want using XSLT 2.0

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

  <xsl:output method="text"/>

  <xsl:template match="Policy">
    <xsl:for-each-group select="//Coverage" group-by="CoverageName">
         <xsl:value-of select="current-grouping-key()" />
         <xsl:text>  </xsl:text>
         <xsl:value-of select="CoverageLimit" />
         <xsl:text>  </xsl:text>
         <xsl:value-of select="sum(current-group()//CoverageAmount)" />
         <xsl:text>&#10;</xsl:text>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>

Here is the output for your sample XML document

ABC  Upto 50,000  200
EFG  Upto 50,000  100
HIJ  Upto 10,000  60
KLM  Upto 100,000  120

It is more difficult to do using an XSLT 1.0 processor but if you have do so I suggest you try the Muenchian grouping method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.