Input:
<root><element>
<small>a</small>
<Large>B</Large>
<Time>301</Time></element><element>
<small>a</small>
<Large>B</Large>
<Time>322</Time></element><element>
<small>b</small>
<Large>A</Large>
<Time>274</Time></element><element>
<small>c</small>
<Large>B</Large>
<Time>325</Time></element><element>
<small>b</small>
<Large>A</Large>
<Time>301</Time></element></root>
Need to write a xslt see how many times the small and Large elements comes in pair and list the count in smallnum tag and also add the time of that many iterations to totsmalltime tag.
Output:
<root><element>
<small>a</small>
<Large>B</Large>
<smallnum>2</smallnum>
<totsmalltime>623</totsmalltime></element><element>
<small>b</small>
<Large>A</Large>
<smallnum>2</smallnum>
<totsmalltime>575</totsmalltime></element><element>
<small>c</small>
<Large>B</Large>
<smallnum>1</smallnum>
<totsmalltime>325</totsmalltime></element></root>
I. XSLT 1.0 solution:
Here is a shorter solution that is completely “push style” 🙂
When this transformation is applied on the provided XML document:
the wanted, correct result is produced:
Explanation:
Proper use and overriding of the identity rule.
Proper use of the Muenchian Grouping Method.
II. XSLT 2.0 Solution:
When this transformation is applied on the same XML document (above), the same correct result is produced:
Explanation:
Proper use of
<xsl:for-each-group>withgroup-byattribute.Proper use of the
current-group()function.