mnvbrtn
mnvbrtn

Reputation: 568

how to sum up with recent date in xsl

Input is as follows:

   <Response>
     <item>
    <dt>20141106</dt>
    <nbr>33456</nbr>
    <cnt>2</cnt>
     </item>
     <item>
    <dt>20141107</dt>
    <nbr>33457</nbr>
    <cnt>1</cnt>
     </item>
      <item>
    <dt>20141207</dt>
    <nbr>33457</nbr>
    <cnt>3</cnt>
     </item>
      <item>
    <dt>20140507</dt>
    <nbr>33458</nbr>
    <cnt>1</cnt>
     </item>
      <item>
    <dt>20141109</dt>
    <nbr>33459</nbr>
    <cnt>1</cnt>
     </item>
      <item>
    <dt>20140407</dt>
    <nbr>33459</nbr>
    <cnt>7</cnt>
     </item>

    </Response>

my output should be as follows with latest dt and sum of the count for each item. i need to add the cnt value for the same nbr and give the latest date value. how to do with stylesheet usin xsl

output looks like

<Response>
 <item>
<dt>20141106</dt>
<nbr>33456</nbr>
<cnt>2</cnt>
 </item>    
  <item>
<dt>20141207</dt>
<nbr>33457</nbr>
<cnt>4</cnt>
 </item>
  <item>
<dt>20140507</dt>
<nbr>33458</nbr>
<cnt>1</cnt>
 </item>
  <item>
<dt>20141109</dt>
<nbr>33459</nbr>
<cnt>8</cnt>
 </item>
</Response>

Upvotes: 1

Views: 64

Answers (1)

Flynn1179
Flynn1179

Reputation: 12075

Try this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="item">
     <xsl:if test="not(../item[nbr = current()/nbr and dt > current()/dt])">
       <xsl:copy>
         <xsl:apply-templates />
       </xsl:copy>
     </xsl:if>
   </xsl:template>

   <xsl:template match="cnt">
     <xsl:copy>
       <xsl:value-of select="sum(../../item[nbr = current()/../nbr]/cnt)"/>
     </xsl:copy>
   </xsl:template>

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

Upvotes: 1

Related Questions