user1905354
user1905354

Reputation: 39

After grouping calculate the sum of first 3 occurance of node using xslt 1.0

How to calculate the sum of first 3 occurance of the value the hour node after grouping by empid and date in the below xml and store the total in a variable.

<Records>
    <Line>
      <EmpId>1</EmpId>
      <Date>10/01/2012</Date>
      <Hour>4</Hour>
    </Line>
    <Line>
      <EmpId>1</EmpId>
      <Date>10/01/2012</Date>
      <Hour>4</Hour>
    </Line>
    <Line>
      <EmpId>1</EmpId>
      <Date>10/02/2012</Date>
      <Hour>8</Hour>
    </Line>
     <Line>
      <EmpId>1</EmpId>
      <Date>10/03/2012</Date>
      <Hour>8</Hour>
    </Line>
     <Line>
      <EmpId>1</EmpId>
      <Date>10/04/2012</Date>
      <Hour>8</Hour>
    </Line>
  </Records>

So the value of the variable should be 24.

Upvotes: 0

Views: 66

Answers (1)

Flynn1179
Flynn1179

Reputation: 12075

If I read your requirement correctly, I think this should do the job:

<xsl:key name="countHours" match="Line" use="concat(EmpId,'-',Date)" />

<xsl:template match="/">
  <xsl:value-of select="sum(
    Records/Line[
      count(
        preceding-sibling::*[
          generate-id()=generate-id(key('countHours',concat(EmpId,'-',Date))[1])
        ]
      ) &lt; 3
    ]/Hour
  )" />
</xsl:template>

Upvotes: 1

Related Questions