Reputation: 39
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
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])
]
) < 3
]/Hour
)" />
</xsl:template>
Upvotes: 1